ES6 箭头函数--特性
如果箭头表达式仅仅就是简化了函数的命名,我们为什么要改变原来的习惯而去使用它呢?所以我们需要了解一下箭头函数的特性。
箭头函数内部没有constructor方法,也没有prototype,所以不支持new操作。但是它对this的处理与一般的普通函数不一样。箭头函数的 this 始终指向函数定义时的 this,而非执行时。我们通过一个例子来理解:
var o = {
x : ,
func : function() { console.log(this.x) },
test : function() {
setTimeout(function() {
this.func();
}, );
}
};
o.test(); // TypeError : this.func is not a function
上面的代码会出现错误,因为this的指向从o变为了全局(函数调用中的this都是指向全局的)。
我们需要修改上面的代码如下:
var o = {
x : ,
func : function() { console.log(this.x) },
test : function() {
var _this = this;
setTimeout(function() {
_this.func();
}, );
}
};
o.test();
通过使用外部事先保存的this就行了。这里就可以利用到箭头函数了,我们刚才说过,箭头函数的 this 始终指向函数定义时的 this,而非执行时。所以我们将上面的代码修改如下:
var o = {
x : ,
func : function() { console.log(this.x) },
test : function() {
setTimeout(() => { this.func() }, );
}
};
o.test();
这回this就指向o了。
我们还需要注意一点的就是这个this是不会改变指向对象的,我们知道call和apply可以改变this的指向,但是在箭头函数中是无效的。
var x = ,
o = {
x : ,
test : () => this.x
}; o.test(); //
o.test.call(o); // 依然是1
ES6 箭头函数--特性的更多相关文章
- ES6 — 箭头函数
一 为什么要有箭头函数 我们在日常开发中,可能会需要写类似下面的代码 const Person = { 'name': 'little bear', 'age': 18, 'sayHello': fu ...
- ES6 箭头函数 this 指向
ES6 箭头函数 this 指向 箭头函数有几个使用注意点: 函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象. 不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个 ...
- es6箭头函数讲解
es6箭头函数的用法 箭头函数是es6的一种函数的简写方法. 如下: var f = v = > v; //等同于 var f = function(v){ return v; } var su ...
- es6箭头函数 this 指向问题
es5中 this 的指向 var factory = function(){ this.a = 'a'; this.b = 'b'; this.c = { a:'a+', b:function(){ ...
- 前端分享----JS异步编程+ES6箭头函数
前端分享----JS异步编程+ES6箭头函数 ##概述Javascript语言的执行环境是"单线程"(single thread).所谓"单线程",就是指一次只 ...
- ES6 箭头函数(Arrow Functions)
ES6 箭头函数(Arrow Functions) ES6 可以使用 "箭头"(=>)定义函数,注意是函数,不要使用这种方式定义类(构造器). 一.语法 具有一个参数的简单函 ...
- ES6箭头函数基本用法
ES6箭头函数基本用法 ``` window.onload = function(){ alert(abc); } //箭头函数 window.onload = ()=>{ alert(&quo ...
- ES6 箭头函数this指向问题
var name = "window"; var person1 = { name: "person1", show1: function() { consol ...
- Vue ES6箭头函数使用总结
Vue ES6箭头函数使用总结 by:授客 QQ:1033553122 箭头函数 ES6允许使用“箭头”(=>)定义函数: 函数不带参数 定义方法:函数名称 = () => 函数体 ...
随机推荐
- python-flask-Flask-SQLAlchemy与Flask-Migrate联合进行数据化迁移
使用步骤: 1. 引入Flask-SQLAlchemy from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() 2. 注册 Flask-SQ ...
- python-flask-script定制manage命令
安装: pip3 install flask-script #!/usr/bin/env python # -*- coding:utf-8 -*- from flask_script import ...
- mysql 全文搜索(转载http://blog.csdn.net/manbujingxin/article/details/6656992)
前提:mysql只支持英文内容的全文索引,所以只考虑英文的全文搜索.假定数据表名为post,有三列:id.title.content.id是自增长序号,title是varchar,content是te ...
- python中列表生成式
1.简介 列表生成式即List Comprehensions,是Python中用于创建list的生成式. 2.示例 [表达式 循环体 条件语句] #!/usr/bin/env python # - ...
- [LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)
描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also kn ...
- textarea标签内的文字无缘故居中解决原因
<textarea> 内容内容 </textarea> 浏览器会解析为 <textarea><br> 内容内容</textarea> ...
- vue中alert toast confirm loading 公用
import Vue from 'vue' import { ToastPlugin, AlertPlugin, ConfirmPlugin, LoadingPlugin } from 'vux' / ...
- !important 的绝对控制样式
<head> <style type="text/css"> div{background-color: blue !important;} </st ...
- php读取excel时间42930转化为时间然后正则验证时间是否通过
excel时间 function exceltimtetophp($days,$time=false) { if(is_numeric($days)) { //凯撒日计数,要把我们运用的从1970年开 ...
- learning ddr seft-refresh mode summary