说说ES6 Class里面的this对象
来源:互联网 发布:打电话软件下载 编辑:程序博客网 时间:2024/06/06 00:24
废话不说,直接上代码,浅显易懂
class Animal { constructor(){ this.type = 'animal' } says(say){ setTimeout(function(){ console.log(this.type + ' says ' + say) }, 1000) }//第一种是将this传给self,再用self来指代thissays1(say){var self = this;setTimeout(function(){console.log(self.type + " says1 " + say);},1000)}//第二种方法是用bind(this),即says2(say){setTimeout(function(){console.log(this.type + " says2 " + say);}.bind(this),1000);}//第三种方法是箭头函数says3(say){ setTimeout( () => { console.log(this.type + ' says ' + say) }, 1000) }} var animal = new Animal() animal.says('hi') //undefined says hi animal.says1('hi') //animal says hi animal.says2('hi') //animal says hi animal.says3('hi') //animal says hi
阅读全文
0 0
- 说说ES6 Class里面的this对象
- JavaScript使用ES6的Class面向对象继承时 this is not defined 解决方法
- ES6箭头函数、Class、this、继承。
- ES6 Class的用法
- 【ES6】class的继承
- 说说聊天室里面的男男女女
- JavaScript 使用对象及ES6中的class
- Jquery里面的this
- js 里面的this
- ES6中class的实现
- ES6--class的基本使用
- ES6对象的扩展
- 【es6】对象的扩展
- ES6--对象的扩展
- ES6对象的扩展
- es6中"类"--class的方式创建对象和es5中正常用法比较
- Android 说说EditText里面的属性
- ES6--Class
- Unity UGUI实现可编辑下拉框(dropdown)
- Spring中ContextLoaderListener和DispatcherServlet的区别
- 主要空间数据挖掘方法
- oracle 解析soap报文XML报错
- Java Thread线程基础总结
- 说说ES6 Class里面的this对象
- Alpha,Beta,RC,GA版本解释
- 丢棋子问题
- oracle下mybatis批量下载的问题
- Python生成PASCAL VOC格式的xml标注文件
- Android用标注管理数据库的简单示例
- .net总结(二)
- Improving Linux kernel networking performance 笔记
- Android 开发之漫漫长途 Ⅲ—Activity 的显示之 Window和View(2)