Home >Web Front-end >JS Tutorial >Why Does a Constructor Function Sometimes Return a Non-this Value?
When invoking a constructor function with the new keyword, it's generally assumed that the function will return the newly created object. However, under specific circumstances, a non-this value can be returned instead.
In JavaScript, the exact condition that governs this behavior is defined in the ECMAScript specification's internal [[Construct]] property for function objects. When called, this property performs the following steps:
Hence, the value returned by the constructor function will determine whether the newly created object is returned or not:
To test if the newly created object is truly different from the one returned by the constructor, one can check: (new Foo() instanceof Foo) === false.
The above is the detailed content of Why Does a Constructor Function Sometimes Return a Non-this Value?. For more information, please follow other related articles on the PHP Chinese website!