[Javascript] this in Function Calls
In most cases, the value of a function's this
argument is determined by how the function is called. This lesson explains what this
refers to when we call plain function. Marius points out how functions behave differently in strict and non-strict mode. "use strict"
mode defaults this
to undefined and prevents us from assigning values to undefined. We must call functions as a constructor to assign their this
value correctly.
"use strict"; console.log(this === global) // false, in REPL this === global
console.log(this === module.exports) // true function Person(firstName, lastName) {
console.log(this === global) // without 'use strict', true; with strict mode, false
console.log(this === undefined) //without 'use strict', false; with strict mode, true
} Person()
Inside a function,
- strict mode, 'this' is undefined
- without strict mode, 'this' is global
"use strict"; console.log(this === global) // false, in REPL this === global
console.log(this === module.exports) // true function Person(firstName, lastName) {
console.log(this === global) // without 'use strict', true; with strict mode, false
console.log(this === undefined) //without 'use strict', false; with strict mode, true
this.firstName = firstName;
this.lastName = lastName;
} const person = new Person("Jane", "Doe");
console.log(person);
console.log(global.firstName); //undefined
console.log(global.lastName); //undefined
[Javascript] this in Function Calls的更多相关文章
- Remote table-valued function calls are not allowed
在SQL Server中,在链接服务器中调用表值函数(table-valued function)时,会遇到下面错误: SELECT * FROM LNK_TEST.TEST.DBO.TEST(12) ...
- JavaScript中的Function(函数)对象详解
JavaScript中的Function对象是函数,函数的用途分为3类: 作为普通逻辑代码容器: 作为对象方法: 作为构造函数. 1.作为普通逻辑代码容器 function multiply(x, y ...
- javascript中的function
function / 对象 所有的变量和方法名的:以字母,$ _开头其他随便,尽量使用英文字母命名,见名知意注意点:不允许使用关键字定义变量和方法的名称====函数即方法,方法即函数====百度:ja ...
- javascript中的function对象
function对象都是Function的实例: > Object.getOwnPropertyNames(Function) [ 'length', 'name', 'arguments', ...
- AsyncCalls – Asynchronous function calls
AsyncCalls – Asynchronous function callsWith AsyncCalls you can execute multiple functions at the sa ...
- 深入理解javascript中的Function.prototye.bind
函数绑定(Function binding)很有可能是你在开始使用JavaScript时最少关注的一点,但是当你意识到你需要一个解决方案来解决如何在另一个函数中保持this上下文的时候,你真正需要的其 ...
- Javascript学习之Function对象详解
JavaScript中的Function对象,就是我们常说的函数对象.在JS中,所有的函数也是以对象的形式存在的. 语法 充当Function对象的构造函数使用,用于结合new关键字构造一个新的Fun ...
- angular-cli项目报Error encountered resolving symbol values statically. Function calls are not supported.错误的处理。
安装同事打包的一个模块,报了这么个错,不过在其他地方使用是正常的. Error encountered resolving symbol values statically. Function cal ...
- javascript中的function命名空間與模擬getter、setter
function的命名空間 在javascript中,function也可以擁有自己的命名空間例如以下這段程式碼: 12345678 function () { return 'I am A';} A ...
随机推荐
- 编译并使用boost库(win7+boost1.63+vs2015+32位or 64位),超详细,boost于vs2017下编译(64/32bit)
首先下载得到boost的最新版(目前最新版是1.63) 下载地址: http://www.boost.org 也可以从这里直接下载 http://download.csdn.net/detail/ ...
- angularjs 路由 ngRoute tab切换
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- 实时监控Cat之旅~对Get和Post进行封装,支持分布式消息树
对第三方接口的调用我们需要对GET和POST进行监控,看一些请求的执行是否成功,如A调用B,B调用C,C调用D,这一连串的东西需要我们使用cat进行记录,进行记录之后,我们可以很容易的发现请求响应的时 ...
- Spring深入浅出(二)IOC的单例 ,继承,依赖,JDBC,工厂模式以及自动装载
IOC的单例模式--Bean Spring中的bean是根据scope来决定的. scope有4种类型: 1.singleton:单例模型,表示通过Spring容器获取的该对象是唯一的.常用并且默认. ...
- 什么是Monad?
为了理解什么是Monad,最好需要了解什么是Monoid.这两篇互为姐妹篇,因为Monad的定义是:A monad is just a monoid in the category of endofu ...
- php八大设计模式之工厂模式
简单点来说,就是用到什么类,就去实例化对应的类.比如:php 可能连接 mysql,可能连接 sqlserver,也可能是 oracle 数据库,可以动态的去链接. 书籍<php权威编程> ...
- MAC下搭建appium UI自动化环境
参考资料: http://qa.blog.163.com/blog/static/190147002201510161119832/ http://blog.csdn.net/liuchunming0 ...
- [NOIP1999]进制位(搜索)
P1013 进制位 题目描述 著名科学家卢斯为了检查学生对进位制的理解,他给出了如下的一张加法表,表中的字母代表数字. 例如: + L K V E L L K V E K K V E KL V V E ...
- GenIcam标准关键词整理
1.<?xml> 版本信息和编码方式 IntSwissKnife 需计算和判断的节点 MaskedIntReg 需查询的节点 2.<RegisterDescription> 寄 ...
- ArcSDE学习笔记------了解ArcSDE
刚来公司的时候一直在做地图服务,用的是ArcGIS,然后对地图的操作用的是普通的数据库操作.后来带我的一个同事让我学习一下ArcSDE.那么ArcSDE到底是什么呢?明明所有的操作我用普通数据库也实现 ...