箭头函数的 this
在非严格模式下,匿名函数和定时器中的 this 由于没有默认的宿主对象,因此指向 window;
而在严格模式下,匿名函数和定时器中的 this 由于没有默认的宿主对象,因此为 undefined。
而 ES6 的箭头函数,他的 this 是继承而来的,默认是定义他的时候的宿主对象。
1、定时器的 this
// (1)、ES5 写法
var obj = {
fun: function() {
var _this = this;
setTimeout(function(){
console.log(_this);
},100);
}
}
obj.fun(); // obj
// (2)、ES6 写法
let object = {
fun: function() {
setTimeout(()=>{
console.log(this);
},100);
}
}
object.fun(); // obj
// (3)、ES6 错误写法
let object1 = {
fun: ()=> {
setTimeout(()=>{
console.log(this);
},100);
}
}
object1.fun(); // window
2、匿名函数的 this
// window
var obj = {
say: function () {
var f1 = function () {
console.log(this); // window, f1调用时,没有宿主对象,默认是window
}
f1();
}
}
obj.say();
// obj
var obj = {
say: function () {
var f1 = ()=> {
console.log(this); // obj
}
f1();
}
}
obj.say();
箭头函数的 this的更多相关文章
- ES6 箭头函数中的 this?你可能想多了(翻译)
箭头函数=>无疑是ES6中最受关注的一个新特性了,通过它可以简写 function 函数表达式,你也可以在各种提及箭头函数的地方看到这样的观点——“=> 就是一个新的 function”. ...
- ES6箭头函数与展开运算符
箭头函数:省去了关键字function和return: eg: reduce=(a,b)=>a+b;//返回a+b的值 redduce=(a,b)=>{console.log(a);con ...
- 箭头函数和Buffer对象
一.箭头函数 普通函数1 var add = function (a, b) { return a + b; } 普通函数2 function add (a, b) { return a + b; } ...
- 深入理解this机制系列第三篇——箭头函数
× 目录 [1]痛点 [2]解决 [3]基本用法[4]回调函数[5]注意事项 前面的话 this机制与函数调用有关,而作用域则与函数定义有关.有没有什么是可以将this机制和作用域联系起来的呢?本文将 ...
- Es6 箭头函数
1.单参数function cheng(a=3){ return a*a;}let cheng= (a=3)=>a*a;console.log(cheng(9));2.多参数functio ...
- 【javascript】箭头函数
ES6标准新增了一种新的函数:Arraw Function(箭头函数). x => x * x 这个函数相当于 function (x){ return x * x; } 题外话:user st ...
- JS中generater和箭头函数
generater跟函数很像: function* fn(x){ yield x; yield x++; return x;} 如上所示,generater用function*定义,可以用yield返 ...
- 箭头函数 Arrow Functions/////////////////////zzz
箭头符号在JavaScript诞生时就已经存在,当初第一个JavaScript教程曾建议在HTML注释内包裹行内脚本,这样可以避免不支持JS的浏览器误将JS代码显示为文本.你会写这样的代码: < ...
- ES6里箭头函数的陷阱
ECMAScript 6新增了箭头函数 原来的匿名函数 function(){},现在可以简化成()=>{} 看起来高大上,像C#什么的语法. 但是箭头函数的this对象,不能更改,总是指向函数 ...
- JavaScript箭头函数 和 generator
箭头函数: 用箭头定义函数........ var fun = x=>x*x alert(fun(2)) //单参数 var fun1 = ()=& ...
随机推荐
- Android Studio 1.5启动出现“SDK Manager: failed to install”问题的解决
问题描述 Android Studio 1.5是当前最新Android手机应用开发平台,下载bundle版安装后,启动Studio后出现“SDK Manager: failed to install” ...
- Probabilistic locking in SQLite
In SQLite, a reader/writer lock mechanism is required to control the multi-process concurrent access ...
- Solr搜索引擎 — 通过mysql配置数据源
一,准备数据库数据表结构 CREATE TABLE `app` ( `id` int(11) NOT NULL AUTO_INCREMENT, `app_name` varchar(255) NOT ...
- cstring 转string
(1)CString转换为string CString cs(_T("cs")); string s; s = (LPCSTR)(CStringA)(cs); (2)string转 ...
- 01网页<head></head>常用标记及属性
网页<head></head>常用标记及属性 <!DOCTYPE html> <html> <head> <!--网页标题--> ...
- 用 Systemtap 统计 TCP 连接
转自: https://mp.weixin.qq.com/s?__biz=MzIxMjAzMDA1MQ==&mid=2648946009&idx=1&sn=3a0be2fe4f ...
- 洛谷——P1120 小木棍 [数据加强版]
P1120 小木棍 [数据加强版] 题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过5050. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍 ...
- bootstrap-table使用笔记
服务端分页: //html <div class="container-fluid"> <div style="margin-top:1em" ...
- 洛谷 4302 BZOJ 1090 SCOI2003 字符串折叠 UVA1630 Folding(输出方案版)
[题解] 区间DP. 设f[i][j]表示i~j的最小代价.再枚举中间点k,很容易想到转移方程为f[i][j]=min(f[i][j],f[i][k]+f[k][j]),同时如果i~k可以通过重复获 ...
- 数位dp备忘录
hdu 4734:把限制值化为数组形式,逐位求解 hdu 4507:类似于上题,其中求平方和的方法很妙 SPOJ BALNUM Balanced Numbers:经典数位dp,注意两点,1,不要把前 ...