In most cases, the value of a function's this argument is determined by how the function is called. This lesson explains what thisrefers 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的更多相关文章

  1. Remote table-valued function calls are not allowed

    在SQL Server中,在链接服务器中调用表值函数(table-valued function)时,会遇到下面错误: SELECT * FROM LNK_TEST.TEST.DBO.TEST(12) ...

  2. JavaScript中的Function(函数)对象详解

    JavaScript中的Function对象是函数,函数的用途分为3类: 作为普通逻辑代码容器: 作为对象方法: 作为构造函数. 1.作为普通逻辑代码容器 function multiply(x, y ...

  3. javascript中的function

    function / 对象 所有的变量和方法名的:以字母,$ _开头其他随便,尽量使用英文字母命名,见名知意注意点:不允许使用关键字定义变量和方法的名称====函数即方法,方法即函数====百度:ja ...

  4. javascript中的function对象

    function对象都是Function的实例: > Object.getOwnPropertyNames(Function) [ 'length', 'name', 'arguments', ...

  5. AsyncCalls – Asynchronous function calls

    AsyncCalls – Asynchronous function callsWith AsyncCalls you can execute multiple functions at the sa ...

  6. 深入理解javascript中的Function.prototye.bind

    函数绑定(Function binding)很有可能是你在开始使用JavaScript时最少关注的一点,但是当你意识到你需要一个解决方案来解决如何在另一个函数中保持this上下文的时候,你真正需要的其 ...

  7. Javascript学习之Function对象详解

    JavaScript中的Function对象,就是我们常说的函数对象.在JS中,所有的函数也是以对象的形式存在的. 语法 充当Function对象的构造函数使用,用于结合new关键字构造一个新的Fun ...

  8. angular-cli项目报Error encountered resolving symbol values statically. Function calls are not supported.错误的处理。

    安装同事打包的一个模块,报了这么个错,不过在其他地方使用是正常的. Error encountered resolving symbol values statically. Function cal ...

  9. javascript中的function命名空間與模擬getter、setter

    function的命名空間 在javascript中,function也可以擁有自己的命名空間例如以下這段程式碼: 12345678 function () { return 'I am A';} A ...

随机推荐

  1. less09 判断语句

    less //.mixin (@a) when (lightness(@a) >= 50%) { //255/2=127.5 // background-color: black; //} // ...

  2. BZOJ 1379 模拟退火

    模拟退火的第一题~ //By SiriusRen #include <cmath> #include <cstdio> #include <algorithm> u ...

  3. POJ 1141 括号匹配 DP

    黑书原题 区间DP,递归输出 不看Discuss毁一生 (woc还真有空串的情况啊) //By SiriusRen #include <cstdio> #include <cstri ...

  4. js一些常用方法

    string 增加 IsNullorEmpty : String.prototype.IsNullOrEmpty = function (r) {    if (r === undefined || ...

  5. dedecms4张关键表解析之1

    虽然dedecms默认共有87张表,但是只有4张最核心,最最要的表. 1.第一张表:dede_arctype  栏目表 dede设计者认为不管存放什么样的数据(文章,商品,电影)都应该属于某个栏目(类 ...

  6. RMAN备份脚本

      单机环境全备   export ORACLE_BASE=/oracle export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1 export ORA ...

  7. Hyper-V 导入与导出虚拟机

    虚拟机的导入与导出功能可以将虚拟机通过文件的方式进行转移,可以将虚拟机的文件复制到活动硬盘,然后带到其他的地点进行导入,这样方便了虚拟机的跨地域的转移.但是有一点要注意,所有要转移的虚拟机都必须处于停 ...

  8. docker切换默认镜像源

    docker切换默认镜像源   基于 debian8 默认安装的 docker 镜像源是在国外,pull 镜像的时候奇慢无比,需要自己手动切换成国内的镜像源. 1. 修改配置文件 docker 默认的 ...

  9. easyui-combobox实现取值范围的联动

    需求:需要用两个combobox来输入一个年月的范围,下拉框的内容从服务器获取.需要实现选中前者后,后者的下拉框中不能显示比前者数值小的:选中后者后,前者的下拉框内容不能显示比后者数值大的 有两个co ...

  10. MPI对道路车辆情况的Nagel-Schreckenberg 模型进行蒙特卡洛模拟

    平台Ubuntu 16.04,Linux下MPI环境的安装见链接:https://blog.csdn.net/lusongno1/article/details/61709460 据 Nagel-Sc ...