对象冒充
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
function
Person(name,age){
this
.name=name;
this
.setName=
function
(name){
this
.name=name;
}
this
.getName=
function
(){
return
this
.name;
}
this
.getInfo=
function
(){
alert(
"name:"
+
this
.name);
}
};
function
Student(name,age){
this
.method=Person;
this
.method(name);
delete
this
.method;
this
.age=age;
this
.setAge=
function
(age){
this
.age=age;
}
this
.getAge=
function
(){
return
this
.age;
}
this
.getInfos=
function
(){
alert(
"name:"
+
this
.name+
",age:"
+
this
.age);
}
}
var
person=
new
Person(
"person"
);
var
student=
new
Student(
"student"
,22);
person.getInfo();
student.getInfos();
|
call 方法方式
call 方法是 Function 对象中的方法,因此我们定义的每个函数都拥有该方法。可以通过函数名来调用 call 方法,call 方法的第一个参数会被传递给函数中的 this,从第 2 个参数开始,逐一赋值给函数中的参数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
function
Person(name,age){
this
.name=name;
this
.setName=
function
(name){
this
.name=name;
}
this
.getName=
function
(){
return
this
.name;
}
this
.getInfo=
function
(){
alert(
"name:"
+
this
.name);
}
};
function
Student(name,age){
Person.call(
this
,name);
this
.age=age;
this
.setAge=
function
(age){
this
.age=age;
}
this
.getAge=
function
(){
return
this
.age;
}
this
.getInfos=
function
(){
alert(
"name:"
+
this
.name+
",age:"
+
this
.age);
}
}
var
person=
new
Person(
"person"
);
var
student=
new
Student(
"student"
,22);
person.getInfo();
student.getInfo();
student.getInfos();
|
apply 方法方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
function
Person(name,age){
this
.name=name;
this
.setName=
function
(name){
this
.name=name;
}
this
.getName=
function
(){
return
this
.name;
}
this
.getInfo=
function
(){
alert(
"name:"
+
this
.name);
}
};
function
Student(name,age){
Person.apply(
this
,
new
Array(name));
this
.age=age;
this
.setAge=
function
(age){
this
.age=age;
}
this
.getAge=
function
(){
return
this
.age;
}
this
.getInfos=
function
(){
alert(
"name:"
+
this
.name+
",age:"
+
this
.age);
}
}
var
person=
new
Person(
"person"
);
var
student=
new
Student(
"student"
,22);
person.getInfo();
student.getInfo();
student.getInfos();
|
原型链方式(无法给构造函数传参数)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
function
Person(){}
Person.prototype.name=
"name"
;
Person.prototype.getInfo=
function
(){
alert(
"name:"
+
this
.name);
}
function
Student(){}
Student.prototype=
new
Person();
Student.prototype.age=0;
Student.prototype.getInfos=
function
(){
alert(
"name:"
+
this
.name+
",age:"
+
this
.age);
}
var
person=
new
Person();
person.name=
"person"
;
var
student=
new
Student();
student.name=
"student"
;
student.age=22;
person.getInfo();
student.getInfo();
student.getInfos();
|
混合方式(推荐)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
function
Person(name){
this
.name=name;
}
Person.prototype.getInfo=
function
(){
alert(
"name:"
+
this
.name);
}
function
Student(name,age){
Person.call(
this
,name);
this
.age=age;
}
Student.prototype=
new
Person();
Student.prototype.getInfos=
function
(){
alert(
"name:"
+
this
.name+
",age:"
+
this
.age);
}
var
person=
new
Person(
"person"
);
var
student=
new
Student(
"student"
,22);
person.getInfo();
student.getInfo();
student.getInfos();
|
版权声明:原创作品,如需转载,请注明出处。否则将追究法律责任
本文转自 梦朝思夕 51CTO博客,原文链接:http://blog.51cto.com/qiangmzsx/1362351