关于js中call/apply的一点认识
2016-06-05 16:27
441 查看
上代码
function Parent(a,b) { console.log(this.a,this.b); // 如果单独调用joke()方法,会出现TypeError: this.joke is not a function,因为在Parent中并没有关于对joke()的定义, //this.joke(); // this.a =a; // this.b =b; this.showname=function(a,b){ console.log('this a parent',a+b,a,b); } } function Child(a,b) { this.a = a; this.b = b; this.joke = function() { console.log('I will tell you a joke ,Father'); } // 从此以后,Child具备召唤父亲的能力,而父亲也具备使用儿子的能力??为什么呢? // 本质:替换了函数的上下文,通俗点,在Parent的函数作用域内,this对象已经指代为Child 对象, // 由此,Parent中的this其实也是Child的代表 // 但是单独使用Parent时,Parent并不具备Child的能力,因为也不清楚其有没有Child,对未来也不知,自然this一片空白 // 见证奇迹的时刻,父子合体==>> Parent.call(this,a,b); } // 实际使用中: var child = new Child(1,2); child.showname(3,4); console.log(child.a,child.b);
相关文章推荐
- Android 6.0运行时权限
- APP性能测试工具Emmagee的使用总结
- android-genymotion模拟器使用
- 完全理解android Activity启动模式LauchMode (深入Activity与任务栈)
- Activity的入场动画合集
- LruCache源码浅析
- ## Unity3D实现粒子光环效果
- 使用 Android Studio自定义View02——图文混排的View
- Android ScrollView真正滑动底部
- Android静态安全检测 -> SharedPreferences任意读写
- 软件开发常用工具介绍
- 第一章 JAVA入门(Android安全模型)
- android登录之后保存账号密码
- IOS7笔记-5、视图控制器生命周期
- android开发笔记之高级主题—传感器的简单应用
- iOS 第三方框架-Masonry的使用
- Android AsyncTask的使用
- iOS_NSNotificationCenter(通知中心)
- Django 中related_name,"%(app_label)s_%(class)s_related"
- 各个Android 市场的开发者的网址