es5和es6中的this指向问题
const test ={
id:2,
a:function(){
var a_this=this;
setTimeout(function(){
console.log('a:',this,a_this)
})
},
a1:()=>{
var a1_this=this;
setTimeout(function(){
console.log('a1:',this,a1_this)
})
},
a11:()=>{
var a11_this=this;
setTimeout(()=>{
console.log('a11:',this,a11_this)
})
},
aa:function(){
var aa_this=this;
console.log('aa:',this,aa_this)
},
aa1:()=>{
var aa1_this=this;
console.log('aa1:',this,aa1_this)
}
}
test.a();
test.a1();
test.a11();
test.aa();
test.aa1();
const tests={
id:'tests',
a_tests:{
id:'a_tests',
a_tests_child:function(){
var a_tests_this = this;
console.log('a_tests_child:',this,a_tests_this)
},
a1_tests_child: () => {
var a1_tests_this = this;
console.log('a1_tests_child:',this,a1_tests_this)
},
a11_tests_child:{
a1111_tests_child:function(){
var a1111_tests_this = this;
console.log('a1111_tests_child:',this,a1111_tests_this)
},
a11112_tests_child: () => {
var a11112_tests_this = this;
console.log('a11112_tests_child:',this,a11112_tests_this)
}
}
}
}
tests.a_tests.a_tests_child();
tests.a_tests.a1_tests_child();
tests.a_tests.a11_tests_child.a1111_tests_child();
tests.a_tests.a11_tests_child.a11112_tests_child();
无论内部嵌套有多深,箭头函数this总是指向了window全局对象
es5和es6中的this指向问题的更多相关文章
- ES5和ES6中的继承
看到一篇写的非常好的关于js继承的文章,其中对构造函数.原型.实例之间的关系的描述十分透彻,故转载作者文章以随时学习,并供大家共同进步! ES5 ES5中的继承,看图: function Super( ...
- ES5和ES6中对于继承的实现方法
在ES5继承的实现非常有趣的,由于没有传统面向对象类的概念,Javascript利用原型链的特性来实现继承,这其中有很多的属性指向和需要注意的地方. 原型链的特点和实现已经在之前的一篇整理说过了,就是 ...
- ES5和ES6中的继承 图解
Javascript中的继承一直是个比较麻烦的问题,prototype.constructor.__proto__在构造函数,实例和原型之间有的 复杂的关系,不仔细捋下很难记得牢固.ES6中又新增了c ...
- ES5与ES6中的继承
ES5继承在ES5中没有类的概念,所以一般都是基于原型链继承,具体的继承方法有以下几种: 父类: function Father (name) { this.name = name || 'sam' ...
- 在ES5实现ES6中的Object.is方法
ES6中对象的扩展里面添加了一个Object.is方法,用于比较两个值是否严格相等.内部计算与 === 行为基本一致.那么我们怎么在不支持这个方法的ES5中实现呢? 首先我们需要搞清楚两点,1:Obj ...
- ES5和ES6中关于import & export的书写方式的区别
ES6中输出变量的写法 情景1:单个变量 输出 export const less = 'less' 引用 import {less} from '../index.js' 情景2:多个变量 输出: ...
- 浅谈es5和es6中的继承
首先给大家介绍下在es5中构造函数的继承 function A(){ 2 //构造函数A 3 this.name="我是A函数"; 4 } 5 6 A.prototype={ 7 ...
- es5和es6中查找数组中的元素
let array = [1,2,3,4,5] //es5 let find = array.filter(function (item){ return item %2 === 0//返回满足条件的 ...
- es5和es6中如何处理不确定参数
场景:求出不定参数的总数和 //利用arguments function sum () { let num = 0 //Array.prototype.forEach.call(arguments,f ...
随机推荐
- k8s 1.9.0-手动安装-2
1 下载etcd新版 https://github.com/coreos/etcd/releases 直接下载k8s的二进制包 https://github.com/kubernetes/kubern ...
- Python---基础-运算符int和range函数
这行代码是什么意思 if not (money<100) money >= 100 -------------------------------假设有x = 1, y = 2, z = ...
- 【leetcode】394. Decode String
题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...
- BZOJ 1596: [Usaco2008 Jan]电话网络 树形DP
挺经典的,细节需要特别注意一下 Code: #include<bits/stdc++.h> using namespace std; #define setIO(s) freopen(s& ...
- JavaScript fetch接口
JavaScript fetch接口 如果看网上的fetch教程,会首先对比XMLHttpRequest和fetch的优劣,然后引出一堆看了很快会忘记的内容(本人记性不好).因此,我写一篇关于fetc ...
- centos环境下安装java环境
1-上java官网下载rpm安装包 jdk-7u80-linux-i586.rpm 2-新建文件夹,并把jdk-7u80-linux-i586.rpm上传到此文件夹下面 cd /usr/local/ ...
- Why is HttpContext.Current null during the Session_End event?
Why is HttpContext.Current null during the Session_End event? On Session_End there is no communicati ...
- JAVA中short和short相加自动转化为int
精度小于int的数值运算的时候都回被自动转换为int后进行计算 所以,下面的代码会报编译错误 short s1 = 1;short s2 = 1;s1= (s1+s2); 必须改成: short s1 ...
- Visual Studio Code-使用Chrome Debugging for VS Code调试JS
准备工作 安装 Debugger for Chrome 插件 按 F5(或选择菜单栏的 Debug->Start Debuging),然后选择 Chrome,就会自动创建默认的配置文件 &quo ...
- leetcode 190. 颠倒二进制位(c++)
颠倒给定的 32 位无符号整数的二进制位. 示例 1: 输入: 00000010100101000001111010011100输出: 00111001011110000010100101000000 ...