Here are the main points to note about various objects:
1. Declaration of the object:
There are two kinds:
var a=new Object ();
Or
var a={
Name= "AA"
}
The first concise and the second direct type. Its assignment is the way it is:
Object name. Property = value
A.value=12
A.name= "AAA"
Wait a minute
2. Objects are reference types and are referenced to each other
For example, Var a={
A.name= "AA"
}
Var b=a;//assigns a object to B
Alert (B.name)
This returns "AA", which indicates that B refers to a. The problem is not this, however, when we change the properties of B:
B.name= "BB"
Then: Alert (A.name)
Will find that the return is "BB"
This means that assigning a to b,b can also manipulate the value of a.
"This is because AB is a reference, they point to the same set of data, as if A is a bridge, leading to the opposite, now B copies the bridge, it can also lead to the opposite." At the same time, the person through the B-bridge to the opposite of a number of operations and through a bridge to the opposite side of the operation essentially no difference, will have an impact on the opposite. 】
3. This in the object
var name = ' The Window ';
var object = {
Name: ' My Object ',
Getnamefunc:function () {
var that=this;
return function () {
Save the object referred to this using the variable that, so that the object of that reference will not change even if the operating environment has changed
return this.name;
}
}
}
Alert (Object.getnamefunc () ());
This is the keyword of JS, has a special meaning, represents the current object, and who is the current object, is determined by the environment in which the function executes.
The objects referred to in this can also change when the environment changes
4. Constructors
function Human (username, age) {
This.username=username;
This.age=age;
This.talk=new Function ("Alert (this.username+ ', ' +this.age)");
}
As the above is a simple constructor, through this function can be simple to create objects:
var a=new Human ("AA", 13)
A.talk ();
You can return a result.
Moreover, this function can be reused, creating many different objects without interference between objects.
var a=new Human ("AA", 13)
var b=new Human ("BB", 13)
var c=new Human ("CC", 13)
5 prototype prototype
function Human (username, age) {
This.username=username;
This.age=age;
This.talk=new Function ("Alert (this.username+ ', ' +this.age)");
}
human.prototype.ww= "Yahaha"
A method for adding properties externally, which is common to all instances.
var a=new Human ("AA", 13)
var b=new Human ("BB", 13)
var c=new Human ("CC", 13)
Alert (AA.WW)
Alert (BB.WW)
6. Object manipulation
function Human (username, age) {
This.username=username;
This.age=age;
This.talk=new Function ("Alert (this.username+ ', ' +this.age)");
}
var a=new Human ("AA", 13)
Alert (aa.age)
Delete A.age
Alert (aa.age)
Will find the first response reply 13, the second one is undefine. Indicates that the property has been deleted.
7. JS Inheritance
1 prototype here the so-called inheritance (more special inheritance), he is a shared property of all instance objects, and the constructor cannot pass parameters.
2 call using the parent function of call (subclass object, parameter 1, parameter 2, Parameter 3 ...) is the most commonly used
3 Apply using the parent class function (subclass object, [Parameter 1, Parameter 2, Parameter 3 ...])
function Human (username, age) {
This.username=username;
This.age=age;
This.introduce=function () {
alert (this.username+this.age);
}
This.sex= ' man ';
}
function Student (username, age, score) {
Calling the parent class constructor for a parameter transfer
Human.call (this, username, age);
This.score=score;
This.username=username;
}
function Teacher (username, age) {
Human.apply (this, [username, age])
}
Student.prototype = new Human (' www ');//Just copy the properties and methods of Human
Teacher.prototype = new Human ();
Even if you write it like this:
Teacher.prototype = new Human ("AAA", 13);
Alert (teacher.age)
and undefine.
Can only be used after re-assignment;
Teacher.age=14
Alert (teacher.age)
The various objects