JavaScript: How does 'new' work internally
2013-05-13 21:22
288 查看
转自:http://stackoverflow.com/questions/6750880/javascript-how-does-new-work-internally
The
uses the internal
and it basically does the following:
Initializes a new native object
Sets the internal
this object, pointing to the Function
If the function's
is not an object (a primitive values, such as a Number, String, Boolean, Undefined or Null),
used instead.
After creating the object, it calls the function, providing the object as its
If the return value of the called function, is a primitive, the object created internally is returned.
Otherwise, if an object is returned, the object created internally is lost.
An equivalent implementation of what the
does, can be expressed like this (assuming that the ECMAScript 5
is available):
The
newoperator
uses the internal
[[Construct]]method,
and it basically does the following:
Initializes a new native object
Sets the internal
[[Prototype]]of
this object, pointing to the Function
prototypeproperty.
If the function's
prototypeproperty
is not an object (a primitive values, such as a Number, String, Boolean, Undefined or Null),
Object.prototypeis
used instead.
After creating the object, it calls the function, providing the object as its
thisvalue.
If the return value of the called function, is a primitive, the object created internally is returned.
Otherwise, if an object is returned, the object created internally is lost.
An equivalent implementation of what the
newoperator
does, can be expressed like this (assuming that the ECMAScript 5
Object.createmethod
is available):
相关文章推荐
- How Does Closure Work in Javascript?
- How does ASM work with RAID where striping and mirroring are already built-in [ID 330398.1]
- How Does Caching Work in AFNetworking? : AFImageCache & NSUrlCache Explained
- How Does The Hacker Economy Work?
- Glenn Doman Method – How Does it Work?
- How Does Excel’s RTD Function Work ?
- How Javascript Timers Work
- How does gdb work?
- How does it work in C#? - Part 3 (C# LINQ in detail)
- How does an app work?
- Singular Value Decomposition (SVD) Tutorial - How does SVD work?
- How exactly does Google AdWords work?
- How Does Caching Work in AFNetworking? : AFImageCache & NSUrlCache Explained
- 读书笔记二 How Does the Internet work?
- How does XVCL work?
- How Does Caching Work in AFNetworking? : AFImageCache & NSUrlCache Explained
- How does google work
- how does startup code work
- How Does SSP Work in Juniper Networks NSM?
- How JavaScript Timers Work [Copy]