There is such code:

'use strict' var collectionProto = { values: function() { return this; }, count: function() { return this.length; }, at: function(index) { return (index > this.length) ? this[index - 1] : null; }, append: function(value) { (isArray(value)) ? this.concat(value): this.push(value); }, removeAt: function(index) { if (index > this.length) { return false; } this.splice(index - 1, 1); return true; } }; function Collection() { //this = []; return []; } Collection.prototype = collectionProto; Collection.prototype.constructor = Collection; var numbers = new Collection(); console.log(numbers.values()); 

Actually, the error itself

TypeError: numbers.values ​​is not a function

And two questions:

  1. Why get out a mistake?
  2. Why it is impossible to write in the constructor this = [] ? After all, this is the object that the default interpreter creates.
  • As I understand it, all the objects that the constructor creates are given a prototype from it. In the returned array, the prototype remains Array.prototype - Semerkin

3 answers 3

  1. You return an array, rather than an object created by the constructor.
  2. this is a service construction, it cannot be redefined. And what's the point?

 'use strict' var collectionProto = { answer: 42 }; function Collection() { this.question = 'The Ultimate Question of Life, the Universe, and Everything'; } Collection.prototype = collectionProto; Collection.prototype.constructor = Collection; var numbers = new Collection(); console.log(numbers.question, numbers.answer); 

  • Those. The objects that I will create in the constructor manually and then return them will not have a prototype, which is defined in the .prototype property of the constructor? It turns out the default prototype can only be set to this object - Semerkin
  • Without commas it is difficult to understand the meaning of your question. this is created by the interpreter itself, the created object is set to it. It is not necessary to return it from the constructor, it will return. In this object ( __proto__ ) a prototype is set from the prototype constructor. - user207618

When you return an object from the constructor, the created object is forgotten. Therefore (in the absence of ES6) you can replace __proto__ (supported in IE11 +):

 'use strict' var collectionProto = Object.assign(Object.create(Array.prototype), { values: function() { return this; }, count: function() { return this.length; }, at: function(index) { return (index > this.length) ? this[index - 1] : null; }, append: function(value) { (isArray(value)) ? this.concat(value): this.push(value); }, removeAt: function(index) { if (index > this.length) { return false; } this.splice(index - 1, 1); return true; } }); function Collection() { var me = []; me.__proto__ = this.__proto__; return me; } Collection.prototype = collectionProto; Collection.prototype.constructor = Collection; var numbers = new Collection(); console.log(numbers.values()); 

     'use strict' var collectionProto = { values: [], count: function() { return this.values.length; }, at: function(index) { return (index > this.values.length) ? this.values[index - 1] : null; }, append: function(value) { Array.isArray(value) ? this.values.concat(value): this.values.push(value); }, removeAt: function(index) { if (index > this.values.length) { return false; } this.values.splice(index - 1, 1); return true; } }; function Collection() {} Collection.prototype = collectionProto; Collection.prototype.constructor = Collection; var numbers = new Collection(); console.log(numbers.values); numbers.append(6757); console.log(numbers.values); 

    • one
      Why in the prototype values , if it is blocked by "its" property? - user207618
    • Cleaning up the variable, just in case) - Dmitry
    • one
      Um ... what? If there is a variable in the object itself, then there will be no access to the variable of the same name in the prototype, all manipulations will take place exactly with its variable. What is there to clean? :) - user207618
    • Hey, for what a negative assessment? For inexperience? Why? The code works. And why is the answer just a demonstration of class work? - Dmitry
    • Unfortunately, it is impossible to force the minus to always explain for what a minus. - user207618