手机
当前位置:查字典教程网 >编程开发 >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中的对象继承】相关文章:

javascript操作ul中li的方法

详谈javascript中的cookie

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

JavaScript数据结构与算法之链表

理解javascript定时器中的单线程

JavaScript中string对象

简单谈谈javascript中this的隐式绑定

javascript中this的四种用法

javascript用函数实现对象的方法

JQuery使用index方法获取Jquery对象数组下标的方法

精品推荐
分类导航