If NewTarget is not undefined, throw a TypeError exception.
Note: the Symbol Constructor is not intended to be used with the new operator(not the target for new)
If description is undefined, let descString be undefined; Else, let descString be ? ToString(description).
Return a new unique Symbol value whose [[Description]] value is descString.
Note: the Symbol() function returns a Symbol type instead of a Symbol object
Symbol Prototype Object
abstract operation thisSymbolValue(value)
If Type(value) is Symbol, return value.
If Type(value) is Object and value has a [[SymbolData]] internal slot
then Return value.[[SymbolData]] //a Symbol Object
Throw a TypeError exception.
Example
mySymbol = Symbol( "my symbol" )
// Object() performs a type conversion when called as a function rather than as a constructor.
mySymObj = Object(mySymbol)
console.log(mySymObj.toString()) // Symbol(my symbol)
function anInstanceOfFunction(data)
{
this.data = data;
}
thisArgument = {"data" : null};
// all instances of Function(function definitions or dynamical functions by `new Function`) has
// their [[prototype]] chained to the Function constructor's prototype property(Function.prototype)
anInstanceOfFunction.call(thisArgument, "dat");
console.log(thisArgument.data);
note Function.prototype itself is a function object(to ensure compatibility with es2015), which returns undefined when called; but the call here is `Function.prototype.` instead of `Function.prototype(`
If IsCallable(F) is false, throw a TypeError exception.//F is anInstanceOfFunction in our example
Pop from the execution context stack//the top execution context has no further use and optimized out
Let calleeContext be PrepareForOrdinaryCall(F, undefined):
base_obj = {
"constructor" : my_constructor,
}
function my_constr_stub()
{
console.log("in constructor stub:new.target=" + new.target);//undefined
}
function my_constructor()
{
console.log("in constructor:new.target=" + new.target);// function my_constructor(){...
my_constr_stub();
}
my_constructor.prototype = base_obj;
new my_constructor()
Object [[Construct]](a List of any, newTarget)
The second argument is the object to which the new operator was initially applied. For example, in following code,
the new operator is initially applied to the Square object:
factit = function factorial(n) {
console.log(n)
if (n < 2) {
return 1;
}
return n * factorial(n - 1);
}
non_recursive = function(){}
/*
can't visit factorial here:
as is indicated in `Runtime Semantics: Evaluation` of NAMED function expression,
we will NewDeclarativeEnvironment(scope) as a middle env to recv factorial
from ecma:
The BindingIdentifier in a FunctionExpression can be referenced
from inside the FunctionExpression's
FunctionBody to allow the function to call itself recursively.
However, unlike in a FunctionDeclaration, the BindingIdentifier in
a FunctionExpression cannot be referenced from and does
not affect the scope enclosing the FunctionExpression.
*/
StatementList : StatementList StatementListItem //StatementListItem can be Statement or Declaration
Let declarations be TopLevelVarScopedDeclarations of StatementList.
Append to declarations the elements of the TopLevelVarScopedDeclarations of StatementListItem.
Return declarations.
StatementListItem : Declaration
if is function Declaration, return a list contains the Declaration// function is not taken as Lex but top level Var!
Return the TopLevelVarScopedDeclarations of LabelledItem.
LabelledItem : Statement
// recursive to the last non-labelled Statement
If Statement is Statement : LabelledStatement , return TopLevelVarScopedDeclarations of Statement
Return VarScopedDeclarations of Statement.
StatementList : StatementList StatementListItem //StatementListItem can be Statement or Declaration
Let declarations be TopLevelLexicallyScopedDeclarations of StatementList.
Append to declarations the elements of the TopLevelLexicallyScopedDeclarations of StatementListItem.
Return declarations.
StatementListItem : Statement
Return a new empty List.//VariableStatement contribute to Var instead of Lex
StatementListItem : Declaration
if Declaration is `let ` or `const `, return a new List containing Declaration;
else Return a new empty List// TopLevel functions are taken as Var instead