本文主要跟大家分享5種js實作繼承的方式實例分享,希望能幫助大家更掌握js繼承方式的實作。
1、繼承第一種方式:物件冒充
#1
2
3
4
5
6
7
#8
9
10
11
12
13
14
15
16
17 18
19
20
21
#22
23
24
|
#1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function Parent(username){
this.username = username;
this.hello = function (){
alert(this.username);
}
}
function Child(username,password){
this.method = Parent;
this.method(username);
delete this.method;
this.password = password;
this.world = function (){
alert(this.password);
}
}
var parent = new Parent( "zhangsan" );
var child = new Child( "lisi" , "123456" );
parent.hello();
child.hello();
child.world();
|
登入後複製 |
2、繼承第二種方式:call()方法方式
call方法是Function類別中的方法
call方法的第一個參數的值賦值給類別(即方法)中出現的this
call方法的第二個參數開始依序賦值給類別(即方法)所接受的參數
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function test(str){
alert(this.name + " " + str);
}
var object = new Object();
object.name = "zhangsan" ;
test.call(object, "langsin" );
function Parent(username){
this.username = username;
this.hello = function (){
alert(this.username);
}
}
function Child(username,password){
Parent.call(this,username);
this.password = password;
this.world = function (){
alert(this.password);
}
}
var parent = new Parent( "zhangsan" );
var child = new Child( "lisi" , "123456" );
parent.hello();
child.hello();
child.world();
|
登入後複製
##
3、继承的第三种方式:apply()方法方式
apply方法接受2个参数,
A、第一个参数与call方法的第一个参数一样,即赋值给类(即方法)中出现的this
B、第二个参数为数组类型,这个数组中的每个元素依次赋值给类(即方法)所接受的参数
1 2 3 #4 #5 ##678910#11#12#13 14151617181920#21222324 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function Parent(username){
this.username = username;
this.hello = function (){
alert(this.username);
}
}
function Child(username,password){
Parent.apply(this, new Array(username));
this.password = password;
this.world = function (){
alert(this.password);
}
}
var parent = new Parent( "zhangsan" );
var child = new Child( "lisi" , "123456" );
parent.hello();
child.hello();
child.world();
|
登入後複製 |
4、继承的第四种方式:原型链方式,即子类通过prototype将所有在父类中通过prototype追加的属性和方法都追加到Child,从而实现了继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function Person(){
}
Person.prototype.hello = "hello" ;
Person.prototype.sayHello = function (){
alert(this.hello);
}
function Child(){
}
Child.prototype = new Person();
Child.prototype.world = "world" ;
Child.prototype.sayWorld = function (){
alert(this.world);
}
var c = new Child();
c.sayHello();
c.sayWorld();
|
登入後複製 |
5、继承的第五种方式:混合方式
混合了call方式、原型链方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function Parent(hello){
this.hello = hello;
}
Parent.prototype.sayHello = function (){
alert(this.hello);
}
function Child(hello,world){
Parent.call(this,hello);
this.world = world;
}
Child.prototype = new Parent();
Child.prototype.sayWorld = function (){
alert(this.world);
}
var c = new Child( "zhangsan" , "lisi" );
c.sayHello();
c.sayWorld();
|
登入後複製 |
希望本文所述对大家JavaScript程序设计有所帮助。
相关推荐:
学习javascript面向对象 javascript实现继承的方式_javascript技巧
js面向对象之继承知识详解
JavaScript实现多重继承实例详解
以上是5種js實作繼承的方式實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!