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:
- Why get out a mistake?
- Why it is impossible to write in the constructor
this = []? After all,thisis the object that the default interpreter creates.