javascript的apply方法总结
来源:互联网 发布:网络淘宝兼职可信吗 编辑:程序博客网 时间:2024/06/05 21:06
apply方法能劫持另外一个对象的方法,继承另外一个对象的属性
<script> function Person(name,age){ //定义一个类,人类 this.name=name; //名字 this.age=age; //年龄 this.sayhello=function(){alert("hello")};} function Print(){ //显示类的属性 this.funcName="Print"; this.show=function(){ var msg=[]; for(var key in this){ if(typeof(this[key])!="function"){ msg.push([key,":",this[key]].join("")); } } alert(msg.join(" ")); };} function Student(name,age,grade,school){ //学生类 Person.apply(this,arguments); Print.apply(this,arguments); // Person.call(this,...arguments);//效果相同 // Print.call(this,...arguments); this.grade=grade; //年级 this.school=school; //学校 } var p1=new Person("jake",10);p1.sayhello();var s1=new Student("tom",13,6,"清华小学");s1.show();s1.sayhello();alert(s1.funcName);</script>学生类本来不具备任何方法,但是在Person.apply(this,arguments)后,
他就具备了Person类的sayhello方法和所有属性。
在Print.apply(this,arguments)后就自动得到了show()方法
原文链接:http://www.jb51.net/article/42705.htm
0 0
- javascript的apply方法总结
- javascript的apply方法
- JavaScript中call,apply,bind方法的总结。
- JavaScript中call,apply,bind方法的总结。
- JavaScript中call,apply,bind方法的总结
- Javascript中call,apply,bind方法的详解与总结
- JavaScript中call,apply,bind方法的总结。
- JavaScript中call,apply,bind方法的总结
- JavaScript中call,apply,bind方法的总结。
- JavaScript中call,apply,bind方法的总结
- JavaScript中call,apply,bind方法的总结
- javascript中的call()方法和apply()方法用法总结
- javascript 的call和apply方法
- javascript 函数的方法call()和apply()
- javascript apply and call方法的区别
- JavaScript 的apply和call方法
- Javascript 方法apply和call的区别
- javaScript apply方法的使用详细解析
- Visual Studio 20周年软件趋势随想
- 直方图内最大矩形
- 如何设计一个手机信息页面?
- 透明度与十六进制代码转换
- 网络爬虫技术
- javascript的apply方法总结
- node百度语音识别
- upload 上传类
- Android_三种菜单介绍
- MySQL中函数CONCAT及GROUP_CONCAT
- JSP页面上实现权限树回显
- Ubuntu16.04LTS 安装搜狗输入法
- Gdb远程调试Linux内核遇到的Bug
- Angular.js 的一些学习资源