Quantcast
Channel: Stephen Belanger - Latest Comments
Viewing all articles
Browse latest Browse all 29

Re: A simple explanation of “new” in Javascript.

$
0
0

Not necessarily; if you attempt to return anything but an object (for example a number), it'll still evaluate to the newly constructed object. `function Foo() {return 42}; (new Foo) instanceof Foo` evaluates to true.

The best way to understand how `new` works would be to read the spec, but it can be a bit difficult to understand sometimes. However, if you actually want to know for sure how it works, I'd suggest checking there: http://es5.github.com/#x13.2.2

8. Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.
9. If Type(result) is Object then return result.
10. Return obj.


Viewing all articles
Browse latest Browse all 29

Trending Articles