3 ms·
In reference to constructor invocation: ...a new empty object is created, (let’s name it newObj) and the function is invoked on that object, like say, newO
by gerso 9y ago
In reference to constructor invocation:
...a new empty object is created, (let’s name it newObj) and the function is invoked on that object,
like say, newObj.Person() in this case.
Is this true? When a function is invoked with "new", "this" is set to the object being constructed, but is that accomplished by making the constructor a method of the object being initialized? If so, it must be removed later on in the process:
(new Function).Function
=> undefined
- hdhzy 9y agoThe algorithm for "var p = new Person" is roughly (ignoring prototypes): var p = {}; Person.call(p); // calls Person with p as this Remember that in js there are no "methods" just functions. The original constructor function can be accessed by "constructor" property: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- laughinghan 9y agoIt is not true. It's just a confusing attempt by the article to explain what happens to `this` inside Person(). If anything, it's more like: var newObj = Object.create(Person.prototype) var result = Person.call(newObj) if (typeof result === 'object' && result !== null) return result return newObj The article didn't explain it like that because that requires introducing the concept of .call() on functions.