1. Some language features related to objects
Everything in JavaScript is an object, and the object is a collection of Attributes. You know, a function is also an object that can be used as the value of a variable, a return value, a parameter, or a Property. A special place for function objects is the ability to execute code contained within the xxx function object through the "xxx ()" syntax .
2. Object properties can be added and deleted dynamically
var foo = new Object ();
add bar Property to Foo object
Foo.bar = "foobar";
Alert (foo.bar); Foobar
Delete foo object's Bar Property
Delete foo.bar;
Alert (foo.bar); Undefined
3. Create objects
JavaScript sets a prototype for each object that is created, pointing to its prototype Object. For example, Create an Array object:
var arr = [1, 2, 3];
its prototype chain Is:
Arr----> array.prototype----> object.prototype----> NULL
When we create a function:
function Foo () {
Return 0;
}
The function is also an object, and its prototype chain Is:
Foo----> function.prototype----> object.prototype----> NULL
4. Constructors Create objects
First define a constructor function:
function Student (name) {
THIS.name = name;
This.hello = function () {
Alert (' Hello, ' + this.name + '! ');
}
}
The function above is a normal function, but the in JavaScript, This function is called with the keyword new, which is the constructor:
var xiaoming = new Student (' xiaoming ');
xiaoming.name; ' Xiao Ming '
Xiaoming.hello (); Hello, xiaoming !
JavaScript objects are created by constructors , and each object has a constructor property that represents the constructor that created the object :
function Test () {THIS.A = "hello";}
var test = new Test (); created by the Test Constructor
Alert (test.constructor);
Alert:function Test () {THIS.A = "hello";}
constructors are also objects , and constructors are created by built- in function functions :
function Test (a, B)
{
Alert (a+b);
}
equivalent to :
Test = new Function (["a", "b"], "alert (a+b);");
Function is an intrinsic object of native code implementation . But for consistency , , Function also have Constructor Properties , This property points to its own.
5. prototype prototype
prototype is a property of the constructor This property points to an object When we use the obj.xxx When you access the properties of an object, the javascript The engine looks for the property on the current object, if it is not found, to its prototype object, if not found, has been traced to the object.prototype undefined
/prototype default to new Object (); for convenience , recorded as P_obj
function Person (name) {
THIS.name = name;
}
Add sayname property for p_obj
Person.prototype.sayName = function () {
Alert (this.name);
}
var john = new Person ("john"); John 's base reference points to p_obj
var Eric = new Person ("eric"); Eric 's base reference also points to p_obj
Note The This in the sayname code points to the instantiated object ( this binding )
John.sayname (); The John object itself does not have a sayname Property , so access to the prototype object p_obj sayname Properties
Eric.sayname (); Accessing the sayname Property of the same prototype object p_obj
The prototype and object mechanism in JavaScript