手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >使用apply方法实现javascript中的对象继承
使用apply方法实现javascript中的对象继承
摘要:复制代码代码如下://使用apply方法实现对象继承functionParent(username){this.username=usern...

复制代码 代码如下:

<script type="text/javascript">

//使用apply方法实现对象继承

function Parent(username) {

this.username = username;

this.sayHello = function() {

alert(this.username);

}

}

function Child(username, password) {

Parent.apply(this, new Array(username));

//和下面一样

//Parent.apply(this, [username]);

this.password = password;

this.sayWorld = function() {

alert(this.password);

}

}

var parent = new Parent("zhangsan");

var child = new Child("lisi", "123");

parent.sayHello();

child.sayHello();

child.sayWorld();

</script>

【使用apply方法实现javascript中的对象继承】相关文章:

仅30行代码实现Javascript中的MVC

在JavaScript中正确引用bind方法的应用

理解javascript定时器中的单线程

理解javascript中的with关键字

javascript用函数实现对象的方法

JavaScript中的bold()方法使用详解

javascript的 {} 语句块详解

详解Javascript中的Object对象

JavaScript正则表达式的分组匹配详解

谈一谈javascript中继承的多种方式

精品推荐
分类导航