I want to know how the new
operator work and not just learn how to use it.I looked in the ECMAScript 5 standard and find the algorithm describe how it work but am a little confused about the meanning of it.
The production
NewExpression : new NewExpression
is evaluated as follows:
- Let
ref
be the result of evaluating NewExpression.- Let
constructor
be GetValue(ref).- If
Type(constructor)
is notObject
, throw aTypeError
exception.- If
constructor
does not implement the[[Construct]]
internal method, throw aTypeError
exception.- Return the result of calling the
[[Construct]]
internal method onconstructor
, providing no arguments (that is, an empty list of arguments).
I try to understand the algorithm above using this example:
var f = function() {};
var h = new f();
Particularly I don't understand the first step and therefore cannot follow the other steps.
- Let
ref
be the result of evaluating NewExpression.
var h = new f();
~~~ ~~~~
| \_________ NewExpression
new operator
Does that mean ref
is the value of f()
? But It's undefined
.
3 . If
Type(constructor)
is notObject
, throw aTypeError
exception.
But the type of f
is function, will it throw a TypeError
exception?
5 . Return the result of calling the
[[Construct]]
internal method onconstructor
, providing no arguments (that is, an empty list of arguments).
[[Construct]]
internal property of function, What's the meaning of calling it on constructor
?