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

2019-12-24,,,

复制代码 代码如下:
<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>

您可能感兴趣的文章:

  • AngularJS中$apply方法和$watch方法用法总结
  • JavaScript中apply方法的应用技巧小结
  • JS中使用apply方法通过不同数量的参数调用函数的方法
  • JavaScript中的call方法和apply方法使用对比
  • 从JQuery源码分析JavaScript函数的apply方法与call方法
  • js中apply方法的使用详细解析
  • javascript中call和apply方法浅谈
  • JavaScript 学习笔记(九)call和apply方法
  • javascript call和apply方法
  • Js apply方法详解

《使用apply方法实现javascript中的对象继承.doc》

下载本文的Word格式文档,以方便收藏与打印。