Object declaration in Javascript

Asked

Viewed 406 times

8

function List(){
   this.listSize = 0;
   this.pos = 0;
   this.dataStore = []; 
};

I can consider this code snippet as creating a List object?

  • Here you can also mark as accepted the answer you think best answered your question.

2 answers

8

No. This is a function. It could be called informally "class List", if the intention is to use it as a construction function, with the operator new. In this use, then yes you will be creating an object like List (or "an instance of List"):

function List(){
   this.listSize = 0;
   this.pos = 0;
   this.dataStore = []; 
};
var objetoLista = new List();

3


Javascript is a multi-paradigm language. Among the supported paradigms is Object Orientation.

You can create objects in various ways. In this form, I create a list object, with the listSize, pos and datastore properties, and a method called myMethod(). It is not a definition of a "class" (defining class as an object model), but only an object. In this form, each instance of List will have a copy of the myMethod function().

function List() {
    this.listSize = 0;
    this.pos = 0;
    this.dataStore = [];
    this.myMethod = function () {
        alert('Este é um método');
    };
}

var list = new List();

The way I use to define a class is through prototypes, where you create a "prototype" of a function, and then create objects that are instances of this prototype.

List = function () {
    this.dataStore = [];
};

List.prototype.listSize = 0;

List.prototype.pos = 0;

List.prototype.myMethod = function () {
    alert('Este é um método');
};

var list = new List();

Using this form you will consume less memory, as the definition of myMethod() (and other variables) is shared among all objects that use this prototype object. In addition, editors who own auto-complete will identify that their object has this function/property :-).

UPDATE: Fixed as per @bfavaretto’s remark and this page Optimizing Javascript Code.

  • 1

    Why did you put the properties in the prototype? The method, ok, goes there. But the properties need to be by instance. The way you did, it gets more confusing, and can cause unwanted effects.

  • The properties are in the prototype to reduce memory usage. The object will use the default value of the prototype until it receives an own value, as the example below (assuming that List is defined in prototype format). https://jsfiddle.net/lucasdamiani/m3svaq8b/ ?

  • The unwanted effect is to manipulate properties of a property that is an object and is in the prototype. For example, in its last block of code, list.dataStore.push(1,2,3) popularizes the very array that is in the prototype.

  • This actually applies to objects assigned by reference, not by value. So your observation is valid. I did not read this link correctly the first time. Optimizing Javascript code, here clearly states that the form I suggested is appropriate, but should not be used for types assigned by reference.

  • Legal Lucas. I commented on these things more because I believe that beginners could get confused, especially those who know the traditional paradigm of OOP, based on classes and not prototypes. Putting an object inside the prototype may be useful, but those who use it need to know what they’re doing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.