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. angularjs 工具方法

    <!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" ...

  2. Pocket英语语法---二、指示代词和不定代词是什么

    Pocket英语语法---二.指示代词和不定代词是什么 一.总结 一句话总结: 指示代词:标识人或事物的代词,用来代替前面已提到过的名词 this.these.that.those不定代词:指代不确定 ...

  3. 英语影视台词---四、Sideways

    英语影视台词---四.Sideways 一.总结 一句话总结:杯酒人生 Sideways,大致意思是“偏离.倾斜.转向…”.很明显中文译名与英文原名并没有什么关联,<杯酒人生>这个名字,其 ...

  4. 36.创建模板mylist

    node.h #pragma once //创建模板 template <class T> class Node { public: T t;//数据 Node *pNext;//指针域 ...

  5. Android Gallery和ImageSwitcher同步自动(滚动)播放图片库

    本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) ...

  6. 《剑指offer》包含min函数的栈

    一.题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数. 二.输入描述 输入栈 三.输出描述 最小值 四.牛客网提供的框架 class Solution { public: ...

  7. github下载速度慢解决方法

    1.获取 global.ssl.fastly地址 访问 http://github.global.ssl.fastly.net.ipaddress.com/#ipinfo 获取cdn域名以及ip地址  ...

  8. php八大设计模式之单例模式

    单例模式的好处: 实例化后只得到一个对象,减少内存的开销. 实现单例模式: 提供一个私有的属性用来存储实例后的对象. 禁止外部实例化对象,提供公共的的方法,返回实例化后的对象. 避免继承此类,然后重写 ...

  9. linux下python3源码安装及卸载

    Linux下Python3的源码编译安装和卸载方法 [日期:2019-06-21] 来源:博客园  作者:wuli潇萧 [字体:大 中 小]     (一)Linux下软件的源码编译安装和卸载方法 L ...

  10. Kubernetes安装配置(包括master和node)

    部署Kubernetes云计算平台,至少准备两台服务器,此处为4台,包括一台Docker仓库: Kubernetes Master节点:192.168.124.20 Kubernetes Node1节 ...