NO--11关于"this"你知道多少
为了更好地理解 this,将 this 使用的场景分成三类:
在函数内部 this 一个额外的,通常是隐含的参数。
在函数外部(顶级作用域中): 这指的是浏览器中的全局对象或者 Node.js 中一个模块的输出。
在传递给eval()的字符串中: eval() 或者获取 this 当前值值,或者将其设置为全局对象,取决于 this 是直接调用还是间接调用。
我们来看看每个类别。
this 在函数中
这是最常用的 this 使用方式,函数通过扮演三种不同的角色来表示 JavaScript 中的所有可调用结构体:
普通函数(this 在非严格模式下为全局对象,在严格模式下为undefined)
构造函数(this 指向新创建的实例)
方法(this 是指方法调用的接收者)
在函数中,this 通常被认为是一个额外的,隐含的参数。
this 在普通函数中
在普通函数中,this 的值取决于模式:
非严格模式: this 是指向全局对象 (在浏览器中为window对象)。
function sloppyFunc() {
console.log(this === window); // true
}
sloppyFunc();
严格模式: this 的值为 undefined。
function strictFunc() {
'use strict';
console.log(this === undefined); // true
}
strictFunc();
也就是说,this 是一个设定了默认值(window或undefined)的隐式参数。 但是,可以通过 call() 或 apply() 进行函数调用,并明确指定this的值:
function func(arg1, arg2) {
console.log(this); // a
console.log(arg1); // b
console.log(arg2); // c
}
func.call('a', 'b', 'c'); // (this, arg1, arg2)
func.apply('a', ['b', 'c']); // (this, arrayWithArgs)
this 在构造函数中
如果通过new运算符调用函数,则函数将成为构造函数。 该运算符创建一个新的对象,并通过this传递给构造函数:
var savedThis;
function Constr() {
savedThis = this;
}
var inst = new Constr();
console.log(savedThis === inst); // true
在JavaScript中实现,new运算符大致如下所示(更精确的实现稍微复杂一点):
function newOperator(Constr, arrayWithArgs) {
var thisValue = Object.create(Constr.prototype);
Constr.apply(thisValue, arrayWithArgs);
return thisValue;
}
this 在方法中
在方法中,类似于传统的面向对象的语言:this指向接受者,方法被调用的对象。
var obj = {
method: function () {
console.log(this === obj); // true
}
}
obj.method();
this 在顶级作用域中
在浏览器中,顶层作用域是全局作用域,它指向 global object(如window):
console.log(this === window); // true
在Node.js中,通常在模块中执行代码。 因此,顶级作用域是一个特殊的模块作用域:
// `global` (不是 `window`) 指全局对象:
console.log(Math === global.Math); // true
// `this` 不指向全局对象:
console.log(this !== global); // true
// `this` refers to a module’s exports:
console.log(this === module.exports); // true
this 在 eval() 中
eval() 可以被直接(通过真正的函数调用)或间接(通过其他方式)调用。
如果间接调用evaleval() ,则this指向全局对象:
(0,eval)('this === window')
true
否则,如果直接调用eval() ,则this与eval()的环境中保持一致。 例如:
// 普通函数
function sloppyFunc() {
console.log(eval('this') === window); // true
}
sloppyFunc();
function strictFunc() {
'use strict';
console.log(eval('this') === undefined); // true
}
strictFunc();
// 构造器
var savedThis;
function Constr() {
savedThis = eval('this');
}
var inst = new Constr();
console.log(savedThis === inst); // true
// 方法
var obj = {
method: function () {
console.log(eval('this') === obj); // true
}
}
obj.method();
与this相关的陷阱
有三个你需要知道的与this相关的陷阱。请注意,在各种情况下,严格模式更安全,因为this在普通函数中为undefined,并且会在出现问题时警告。
陷阱:忘记new操作符
如果你调用一个构造函数时忘记了new操作符,那么你意外地将this用在一个普通的函数。this会没有正确的值。 在非严格模式下,this指向window对象,你将创建全局变量:
function Point(x, y) {
this.x = x;
this.y = y;
}
var p = Point(7, 5); // 忘记new!
console.log(p === undefined); // true
// 创建了全局变量:
console.log(x); // 7
console.log(y); // 5
幸运的是,在严格模式下会得到警告(this === undefined):
function Point(x, y) {
'use strict';
this.x = x;
this.y = y;
}
var p = Point(7, 5);
// TypeError: Cannot set property 'x' of undefined
陷阱:不正确地提取方法
如果获取方法的值(不是调用它),则可以将该方法转换为函数。 调用该值将导致函数调用,而不是方法调用。 当将方法作为函数或方法调用的参数传递时,可能会发生这种提取。 实际例子包括setTimeout()和事件注册处理程序。 我将使用函数callItt() 来模拟此用例:
/**类似setTimeout() 和 setImmediate() */
function callIt(func) {
func();
}
如果在非严格模式下把一个方法作为函数来调用,那么this将指向全局对象并创建全局变量:
var counter = {
count: 0,
// Sloppy-mode method
inc: function () {
this.count++;
}
}
callIt(counter.inc);
// Didn’t work:
console.log(counter.count); // 0
// Instead, a global variable has been created
// (NaN is result of applying ++ to undefined):
console.log(count); // NaN
如果在严格模式下把一个方法作为函数来调用,this为undefined。 同时会得到一个警告:
var counter = {
count: 0,
// Strict-mode method
inc: function () {
'use strict';
this.count++;
}
}
callIt(counter.inc);
// TypeError: Cannot read property 'count' of undefined
console.log(counter.count);
修正方法是使用bind():
var counter = {
count: 0,
inc: function () {
this.count++;
}
}
callIt(counter.inc.bind(counter));
// 成功了!
console.log(counter.count); // 1
bind()创建了一个新的函数,它总是能得到一个指向counter的this。
陷阱:shadowing this
当在一个方法中使用普通函数时,很容易忘记前者具有其自己this(即使其不需要this)。 因此,你不能从前者引用该方法的this,因为该this会被遮蔽。 让我们看看出现问题的例子:
var obj = {
name: 'Jane',
friends: [ 'Tarzan', 'Cheeta' ],
loop: function () {
'use strict';
this.friends.forEach(
function (friend) {
console.log(this.name+' knows '+friend);
}
);
}
};
obj.loop();
// TypeError: Cannot read property 'name' of undefined
在前面的例子中,获取this.name失败,因为函数的this个是undefined,它与方法loop()的不同。 有三种方法可以修正this。
修正1: that = this。 将它分配给一个没有被遮蔽的变量(另一个流行名称是self)并使用该变量。
loop: function () {
'use strict';
var that = this;
this.friends.forEach(function (friend) {
console.log(that.name+' knows '+friend);
});
}
修正2: bind()。 使用bind()来创建一个this总是指向正确值的函数(在下面的例子中该方法的this)。
loop: function () {
'use strict';
this.friends.forEach(function (friend) {
console.log(this.name+' knows '+friend);
}.bind(this));
}
修正3: forEach的第二个参数。 此方法具有第二个参数,this值将作为此值传递给回调函数。
loop: function () {
'use strict';
this.friends.forEach(function (friend) {
console.log(this.name+' knows '+friend);
}, this);
}
最佳实践
从概念上讲,我认为普通函数没有它自己的this,并且想到上述修复是为了保持这种想法。 ECMAScript 6通过箭头函数支持这种方法 - 没有它们自己的this。 在这样的函数里面,你可以自由使用this,因为不会被屏蔽:
loop: function () {
'use strict';
// The parameter of forEach() is an arrow function
this.friends.forEach(friend => {
// `this` is loop’s `this`
console.log(this.name+' knows '+friend);
});
}
我不喜欢使用this作为普通函数的附加参数的API:
beforeEach(function () {
this.addMatchers({
toBeInRange: function (start, end) {
...
}
});
});
将这样的隐含参数变成明确的参数使得事情更加明显,并且与箭头函数兼容。
beforeEach(api => {
api.addMatchers({
toBeInRange(start, end) {
...
}
});
});
NO--11关于"this"你知道多少的更多相关文章
- 地区sql
/*Navicat MySQL Data Transfer Source Server : localhostSource Server Version : 50136Source Host : lo ...
- WinForm 天猫2013双11自动抢红包【源码下载】
1. 正确获取红包流程 2. 软件介绍 2.1 效果图: 2.2 功能介绍 2.2.1 账号登录 页面开始时,会载入这个网站:https://login.taobao.com/member/login ...
- C++11特性——变量部分(using类型别名、constexpr常量表达式、auto类型推断、nullptr空指针等)
#include <iostream> using namespace std; int main() { using cullptr = const unsigned long long ...
- CSS垂直居中的11种实现方式
今天是邓呆呆球衣退役的日子,在这个颇具纪念意义的日子里我写下自己的第一篇博客,还望前辈们多多提携,多多指教! 接下来,就进入正文,来说说关于垂直居中的事.(以下这11种垂直居中的实现方式均为笔者在日常 ...
- C++ 11 多线程--线程管理
说到多线程编程,那么就不得不提并行和并发,多线程是实现并发(并行)的一种手段.并行是指两个或多个独立的操作同时进行.注意这里是同时进行,区别于并发,在一个时间段内执行多个操作.在单核时代,多个线程是并 ...
- CSharpGL(11)用C#直接编写GLSL程序
CSharpGL(11)用C#直接编写GLSL程序 +BIT祝威+悄悄在此留下版了个权的信息说: 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharp ...
- ABP(现代ASP.NET样板开发框架)系列之11、ABP领域层——仓储(Repositories)
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之11.ABP领域层——仓储(Repositories) ABP是“ASP.NET Boilerplate Proj ...
- C++11 shared_ptr 智能指针 的使用,避免内存泄露
多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...
- C++11网络编程
Handy是一个简洁优雅的C++11网络库,适用于linux与Mac平台.十行代码即可完成一个完整的网络服务器. 下面是echo服务器的代码: #include <handy/handy.h&g ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(11)-系统日志和异常的处理①
系列目录 系统需要越来越自动化,我们需要引入日志记录和异常捕获管理员的操作记录需要被记录,看出哪些模块是频繁操作,分析哪些是不必要的功能,哪些是需要被优化的.系统的异常需要被捕获,而不是将系统出错显示 ...
随机推荐
- Python中网络编程对 listen 函数的理解
listen函数的第一个参数时SOCKET类型的,该函数的作用是在这个SOCKET句柄上建立监听,至于有没有客户端连接进来,就需要accept函数去进行检查了,accept函数的第一个参数也是SOCK ...
- hihocoder [Offer收割]编程练习赛61
[Offer收割]编程练习赛61 A:最小排列 给定一个长度为m的序列b[1..m],再给定一个n,求一个字典序最小的1~n的排列A,使得b是A的子序列. 贪心即可,b是A的子序列,把不在b中的元素, ...
- 理解Flux架构
本文摘自<Flux架构入门教程>和<谈一谈我对 React Flux 架构的理解>.也有自己的观点和总结.转载请注明出处. 一.Flux架构描述 1. Flux是什么 Flux ...
- ubuntu下boot分区空间不足问题的解决方案
https://blog.csdn.net/along_oneday/article/details/75148240 先查看当前内核版本号(防止误删) uname –r 查看已经安装过的内核 dpk ...
- 蓝桥杯-k倍区间
http://lx.lanqiao.cn/problem.page?gpid=T444 问题描述 给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, Ai+1, .. ...
- 带着问题学习openstack
1.为什么要引入nova conductor, nova conductor 为什么接管了nova api RPC调用nova scheduler 的任务? nova-conductor:与数据库交互 ...
- Hdu4952 - Number Transformation - 数论(2014 Multi-University Training Contest 8)
寻找1~k内i的倍数.则这个数能够看成i*x,则下一个数为(i+1)*y,(i+1)*y>=i*x,那么能够推出.y=x-x/(i+1); 那么当x<i+1时,y==x.之后的循环也不会改 ...
- 【VSC】我安装了哪些扩展插件
Nodejs gitk —— 版本实时比对 Debugger for Chrome —— 让 vscode 映射 chrome 的 debug功能,静态页面都可以用 vscode 来打断点调试. ...
- JS form跳转到新标签页并用post传参
通过js实现跳转到一个新的标签页,并且传递参数.(使用post传参方式) 1 超链接<a>标签 (get传参) <a href="http://www.cnblogs. ...
- python3爬虫-通过selenium获取到dj商品
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.c ...