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

最新文章

  1. 【翻译】如何给tomcat配置memcached-session-manager
  2. TOMCAT的安装部署配置(配图解)
  3. 几种连接数据库的OLEDB驱动程序
  4. C/C++ 快速排序 quickSort
  5. iOS开发常用国外网站清单
  6. ASP.NET中的MD5加密
  7. 最终结算“Git Windowsclient保存username与password”问题
  8. AngularJS2基本构造
  9. cocoa pods 命令不执行command not found
  10. 堆排序—Java
  11. 【48】java抽象类和接口的定义和区别
  12. 爬虫_电影天堂 热映电影(xpath)
  13. Java基础(认识Java)
  14. BootStrap学习从现在开始
  15. java.io.InvalidClassException
  16. IO测试工具之fio详解(转)
  17. Andriod(3)——Understanding Android Resources
  18. 关于目前自己iOS项目使用的插件
  19. 43、Java动态代理一——动态类Proxy的使用
  20. asp.net core microservices 架构之 分布式自动计算(一)

热门文章

  1. Pleasant sheep and big big wolf
  2. 使用MyEclipse编写Java程序
  3. springboot 测试类,项目使用shiro时报错UnavailableSecurityManagerException
  4. [ SQLServer ] 數字類型的欄位細節 - 轉載
  5. tensorboard 使用
  6. Centos安装masscan
  7. MySQL好弱智的一个错误
  8. js cookie 页面倒计时
  9. struts2.x + Tiles2.x读取多个xml 配置文件
  10. android 推断是否支持闪光灯