您的位置:首页 > 移动开发

使用apply方法实现javascript中的对象继承

2013-12-17 11:31 886 查看
javascript中的对象继承的方法有很多,在接下来的文章中为大家介绍下使用apply方法是如何实现的

代码如下:

<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]);

unity3d教程http://www.unitymanual.com/

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: