For example, there is the following code:

function Test (name) { this.name = name; return this; } Test.prototype.setName = function (newName) { this.name = newName; }; new Test(); new Test(); new Test(); 

Each time new Test () is called, a new object is created with the name property. These are completely independent objects. Question: Is there a prototype object for each such new object (in this case three simple objects and for each prototype) or is it always the same for each created object? And if he is always alone, how can you check it?

  • the object is one and is in Test.prototype - a link to this object is added to the objects themselves, it is very easy to check, for example, add a field to the prototype and compare its value with all created objects, or just get prototypes of the created objects and just compare them - Grundy
  • one
    Object.getPrototypeOf() - Dmitriy Simushev

3 answers 3

The prototype object is one and is in the prototype function field.

A link to this object is added to the objects themselves.

For verification, you can get prototypes of the created objects using the Object.getPrototypeOf method and then compare the resulting objects:

 function Test(name) { this.name = name; return this; } Test.prototype.setName = function(newName) { this.name = newName; }; var objs = [new Test('a'), new Test('b'), new Test('c')]; //проверка и вывод, что прототипы объектов являются объектом Test.prototype objs.forEach(function(el) { console.log(el.name, Object.getPrototypeOf(el) === Test.prototype) }); //проверка и вывод, что прототипы объектов равны между собой var prototypes = objs.map(function(el) { return Object.getPrototypeOf(el); }); console.log(prototypes[0] === prototypes[1]); console.log(prototypes[1] === prototypes[2]); console.log(prototypes[0] === prototypes[2]); 

    In order for new objects to automatically install a prototype, the prototype property is set to the constructor.

    When creating an object through new, a reference from the prototype of the constructor function is written into its proto prototype.

    The prototype property makes sense only with the constructor. A property with the name prototype can be specified on any object, but it has a special meaning only if it is assigned to the constructor function.

    By itself, without calling the new operator, it does nothing at all, its only purpose is to specify proto for new objects.

    The value of prototype can only be an object. Technically, anything can be written to this property.

    However, when new is run, the prototype property will be used only if it is an object. A primitive value, such as a number or string, will be ignored.

    For an arbitrary function, let's call it Person, the following is true:

    The prototype of proto new objects created by new Person can be set using the Person.prototype property. The default value of Person.prototype is an object with a single constructor property containing a reference to Person. It can be used to get the function that created it from the object itself. However, JavaScript does not support the correctness of this property, so the programmer can change or delete it. The modern Object.create (proto) method can be emulated with prototype if you want it to work in IE8.

    Answer your question - The prototype is put once it is easy to check

     var test1 = new Test(); var test2 = new Test(); console.dir(test1); console.dir(test2); 

    See their property __ proto__

    https://learn.javascript.ru/new-prototype

    • The name field is its own for each object, it does not belong to the prototype - Grundy

    Supplement answers

    Check that the prototype is always the same:

     function test(){} test.prototype = { arr: [], val: 1 } var a = new test(); var b = new test(); console.log( a.val, a.arr ); // 1, [] console.log( b.val, b.arr ); // 1, [] a.arr.push( 1 ); a.__proto__.val++ //числа не мутабельны, при изменении происходит присваивание нового значение в объект //поэтому если нужно поменять "общий" параметр нужно это делать через прототип console.log( a.val, a.arr ); // 2, [1] console.log( b.val, b.arr ); // 2, [1] console.log( test.prototype.val, test.ptototype.arr ); // 2, [1] a.val++; // создась новое поле в "a" console.log( a.val, a.arr, a.__proto__.val ); // 3, [1], 2 console.log( b.val, b.arr ); // 2, [1]