function func(){ //A Function object would be created
alert("hello,world");
}
var a = new func(); //An instance of function object - func is created
a function associated with an object with a property.
An object is an unordered collection of properties each with zero or more attributes that determine how each property can be used. See more
Reference type is not a language data type. It is used for expository purposes. A Reference is a reference to a property of an object which has two parts - base object and property name.
This denotes an internal property which can be not accessed by javascript code.
This internal property is kind of special. The prototype chain is constructed based on value of this property.
This property is also special. Value of property [[Prototype]] of child objects is set to value of this property.
It is a native object.
These objects child objects of Function. They are created using [function definition syntax or expression new Function(…).
They are child objects of Function objects.
function func(){ //A Function object would be created
alert("hello,world");
}
var a = new func(); //An instance of function object - func is created
Source text is represented as a sequence of character in the Unicode encoding, using the UTF-16 format. Non-Latin Unicode characters are allowed in identifiers, string literals, regular expression literals and comments. In string literals, regular expression literals and identifiers, any character can be expressed as a Unicode escape sequence in the format \uxxxx (\u followed by four hexadecimal digits). It contributes one character to the value. Format control characters are moved from source text before applying the lexical grammar. One must use a escape sequence to include a format-control character in a string or regular expression literals.
In Javascript, there are two kinds of types - primitive types and Object.
This type has exactly one value, called undefined. undefined represents that variable has not been assigned a value.
This type has exactly one value, called null. null represents the null, empty or non-existent reference.
Represents logical entity. It consists of two values - true and false.
A set of number values. Each number value directly represents a number - double-precision 64-bit format IEEE 754 (including NaN, positive infinity and negative infinite). Positive infinity is produced by expression +Infinity and negative infinity is produced by expression -infinity.
set of all string values. A string value is a finite ordered sequence of zero or more 16-bit unsigned integer values.
An object is an unordered collection of properties each with zero or more attributes that determine how each property can be used. Properties are containers that hold (1) other objects, (2) primitive values, or (3) methods. Every property can have zero or more attributes from the list: ReadOnly, DontEnum, DontDelete, and Internal.
function(object that has property [[Call]]): they can be invoked with arguments.
constructor(object that has property [[Construct]]): intended for use with new operator.
What?
A constructor is a Function object that creates and initializes objects.
All constructors are objects, but not all objects are constructors. Each
constructor has a Prototype property that can be used to implement
property-based inheritance and shared properties.
We can assume constructor has an internal property [[Construct]].
How to use?
Objects are created by using constructors in new expressions. Invoking a constructor without using new has consequences that depend on the constructor.
Inheritance
Every constructor has an associated property - prototype, and every object
created by that constructor has an implicit reference to the prototype. It
can be accessed using <constructor>.prototype. A prototype may have a
non-null implicit reference its prototype. This is called prototype chain.
So properties owned by an object’s prototype are shared by all objects sharing
the prototype.
Note: there is no implicit prototype link between constructor and its
prototype. The link is explicit. In prototype chain, all prototype links are
implicit.
Property lookup
When a reference is made to a prototype in an object, the first object in the prototype chain that contains a property of that name wins.
vs. class-based object-oriented language
The state and methods are carried by objects and structure, behavior, and state are all inherited.
alternative 1
function func(params){
function body;
}
var func = function(params){
function body;
}
create a Function object (see here)
return the object
var varfunc = function func(params){
function body;
}
Create a new object using new Object(). We call it O
Add O to the front of the scope chain
Create a new Function object according to this. We call it F
Add a property to O
Remove O from the scope chain
return F
It seems that the object O created in step 1 is useless because it is dropped without being returned to the caller. It is similar to alternative 2.
Create a new object called F.
Set [[Class]] property of F to "Function"
Set [[Prototype]] property of F to Function prototype.
Set [[Call]] property of F according to this
Set [[Construct]] property of F according to this
Set [[Scope]] property of F to a new scope chain that contains the same objects as caller’s Scope.
Set length property to the number of formal parameters.
Create a new object using new Object() (not Object!!!) called P.
Set constructor property of P to F.
Set prototype property of F to P
Note: This is also done when expression var f=new func(parameters) is evaluated.
This is called when the user creates an object using expression new FunctionObject().
create a new native object called F
set [[Class]] property of F to "Object"
Get prototype property of F called P
If P is an object, set [[Prototype]] property of F to P
If P is not an object, set [[Prototype]] property of F to Object.property
Invoke the [[Call]] property of F, providing F as this value. Return value is called R
If R is Object, return it
else return F
Note: because prototype chain is set up before execution of function code, those properties of objects comprising the prototype chain can be used in function body.
If the [[Call]] property is not empty, the Function object can be called as a function. Execution steps include
establishes execution context and enters execution context.
executes function body
exit execution context
return
Every execution context has a scope chain.
Scope chain
A list of objects that are searched when evaluating an identifiers. It is created and initialized when control enters a new execution context. During execution within execution context, the scope chain is affected only by with statements and catch clauses.
Variable object and its instantiation
Every execution context has a variable object.
Note: initialization of the variable object occurs before execution of the code in the execution context.
The object is initialized in following ways
For each parameter, create a property of the variable whose name is the identifier. The values are supplied by the caller as arguments to [[Call]].
If number of passed arguments is less than that of formal parameters, extra parameters have value undefined.
If a function or constructor is given more arguments than required, the behavior is undefined (implementation-dependent).
If multiple parameters have the same name, value of the last one wins even if its value is not supplied by the caller.
function func(param, param){
alert(param);
}
func(10); //alert 'undefined'
func(10,20); //alert 20
create a property whose name is identifier in the function declaration, and whose value is the result returned by creating a Function object. Add this property to the variable object. Replacing the existing property if any.
function func(){
innerfunc(); //legal!!
function innerfunc(){
alert("inner function");
}
}
func();
This step is executed after the creation of the function parameter list and function declaration properties. Create a property whose name is the identifier in variable declaration and whose value is +undefined. Then add the property to the variable object. + If a property with the name already exists, the value of the property and its attributes are NOT changed. So this means if the declared variable has the same name as a declared function or parameter of current function, the original property would not change.
function test(){
//variable a exists here, but its value is undefined.
alert(a+"world"); //so this statement is legal!!
var a = "hello";
}
test();
function test(){
//variable a does not exist at all.
alert(a+"world"); //illegal!! It should throw exception.
}
test();
which object is used as variable object;
what attributes are used for the properties.
See here for details.
Every function and constructor call enters a new execution context. Every return exits an execution context. When control enters an execution context,
Detailed steps depend on type of the code.
| type of code | scope chain | variable instantiation | this |
|---|---|---|---|
Global code |
contains the global object and no others |
uses global object as the variable object and uses property attributes DontDelete. |
the global object |
Eval code |
contains the same objects as the calling context’s scope chain. If null, uses global object. |
uses the calling context’s variable object and uses empty property attributes. |
the same as the this value of the calling context |
Function code(does not include body of nested function declarations) |
contain the activation object followed by the objects in the scope chain stored in the Scope property of the Function object |
uses activation object as the variable object and uses property attributes DontDelete. |
The caller provides the this value. If it is not an object(including the case it’s null), this value is the global object |
Two objects are automatically created when entering function execution context.
Arguments object
When control enters function code, an arguments object is created and initialized. It has properties callee(Function object being executed) and length(number of actual parameters). For each integer i between 0 (inclusive) and length(exclusive), a property with name ToString(i) is created. So you can access the parameter with expression arguments[i].
Activation object
When control enters function code, an activation object is created and associated with the execution context. It is initialized with arguments property and property attributes are DontDelete. Client code can access properties of activation object, but not the object itself. If the call operation is applied to a method of activation object, this value is null.
Usually, unless specified, type of a built-in object is "Function" if that object has a [[Call]] property or "Object" otherwise.
Global
There is a unique global object which is created initially before execution of the program. It contains both built-in objects (Math, String, Date, parseInt…) and additional host defined properties.
NOTE: Built-in properties of global object have attributes DontEnum.
Object
Function
Array
String
Boolean
Number
Math
Date
RegExp
Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError and URIError
But most of the built-in functions don’t have a prototype property.
Every built-in function/constructor has Function prototype as the value of its [[Prototype]] property.
Every built-in prototype object () has Object prototype as the value of its [[Prototype]] property, except Object prototype itself.
Every built-in Function object (whether as a constructor, a function, or both) has a length property whose value is number of named arguments including optional arguments. For example, Array.prototype.slice.length. length property has the attributes ReadOnly, DontDelete, DontEnum(no others). Other properties have attribute DontEnum mainly.
If an object is not a constructor, usually property prototype does not make much sense. Prototype prototype of an object is shared by its child objects. Non-constructor can not have child objects.
| Object | [[Construct]] | [[Call]] | [[Prototype]] | [[Class]] | prototype |
|---|---|---|---|---|---|
Global |
None |
None |
implementation dependent |
implementation dependent |
None? |
Object |
create an object |
perform type conversion |
"Object" |
||
Null |
Null |
Null |
"Object" |
Null |
|
initializes a new Function object[1] |
creates and initializes a new Function object |
"Function" |
Function prototype |
||
Seems not Null. No specification was found in E262-3. |
"Function"[2] |
Null |
|||
Function objects[3] |
see here |
see here |
"Function" |
new Object() |
|
Array |
Initializes the newly created object |
Creates and initializes a new Array object.[4] |
"Function" |
Array prototype |
|
Array prototype |
Null |
Null |
"Array" |
||
String |
intializes the newly created object |
performs a type conversion |
"Function" |
String prototype |
|
String prototype |
Null |
Null |
Object prototype |
"String" |
[2] Function.prototype is a Function object itself. When invoked, it accepts any arguments and returns undefined.
[3] They are child objects of object Function. So each object represents a declared function. Each object can be used to construct child objects using expression new FunctionName(…) or called as a function.
object.property is equivalent to object[property-string] For example, object[name] is equivalent to object["name"].
var variable = new ObjName(parameters);
Get the object referenced by ObjName called O
Call the [[Construct]] method on O
If ObjName references a Function object, see here
var returnValue = functionRef(parameters);
Get the object referenced by functionRef called F
Get value of this
If functionRef is a Reference, get the base object called B
if B is an activation object, set this to null
else set this to B
else set this to null
Call the [[Call]] method on F with this and passed-in parameters. See more
Return result of step 3)
Any elements may be elided anywhere in the array initializer.
var = new Array[1,2,,4];
length of the array is 4 instead of 3. The third element is undefined.
var obj = {}
var obj2 = {name: "myname"}
Values are evaluated each time the object initializer is evaluated.
A new object is created using new Object()
Internal properties [[Prototype]], [[Call]], [[Construct]], etc. are initialized according to the new expression.
Properties are added to this object.
See https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/typeof_Operator. Note: the result of applying typeof operator to Null type is object!! In other words, typeof null returns string object.
equal(==)
strict equal(\===)