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
32
33
|
js 本没有继承,只用js语法来实现继承效果<br> function
Person(age, name)
{
this .name =name;
this .age = age;
}
Person.prototype.sayHi = function
() {
alert( "age=" + this .age+ ",name" + this .name);
}
var
p1 = new
Person(12, "ff" );
var
p2 = new
Person(21, "fffs" );
alert(p1.sayHi == p2.sayHi);
function
Father(name, age)
{
this .name = name;
this .age = age;
}
Father.prototype.sayHi = function
() {
alert( this .name+ "," + this .age);
}
function
Son(name,age,gender)
{
this .gender = gender;
//call调用自己本身
Father.call( this , name, age); //"继承" "父类"的成员
}
// Son.prototype = Father.prototype; //可以用父类的方法(这里有指针的问题)
Son.prototype = new
Father(); //得到父类对象
Son.prototyp.fungame = function
() { }; //prototype也是一个对象
var
f1 = new
Father( "JamesZou" , 29);
f1.sayHi();
var
s1 = new
Son( "jameszou" , 1, true );
|