博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Javascript] this in Function Calls
阅读量:7018 次
发布时间:2019-06-28

本文共 1398 字,大约阅读时间需要 4 分钟。

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 === globalconsole.log(this === module.exports) // truefunction 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 === globalconsole.log(this === module.exports) // truefunction 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); //undefinedconsole.log(global.lastName); //undefined

 

转载地址:http://vfzxl.baihongyu.com/

你可能感兴趣的文章
sublime text3 自动编译php 适合用于简单的php文件执行
查看>>
git分支管理
查看>>
玩转Google开源C++单元测试框架Google Test系列(gtest)之七 - 深入解析gtest
查看>>
C#代码生成工具:文本模板初体验 Hello,World!
查看>>
[WinAPI] API 11 [创建目录]
查看>>
(C#)多线程-BackgroundWorker组件
查看>>
设计工作-Axure
查看>>
6.4. branch
查看>>
win7下Qt5使用mysql C++编程配置
查看>>
OK335xS PMIC(TPS65910A3A1RSL) reset
查看>>
CentOS6.5下安装JDK
查看>>
Webdis内部解析
查看>>
21.7. SNMP
查看>>
[LeetCode] Plus One
查看>>
Android SDK 中文 (56) —— ViewFlipper
查看>>
你必须要知识的架构知识~第四章 抽象类展现代码的层次感
查看>>
《基于MFC的OpenGL编程》Part 6 Keyboard and Mouse Control
查看>>
Java 实现的各种经典的排序算法小Demo
查看>>
DZ验证码不显示等
查看>>
Android 借助Stetho在Chrome上调试Android网络、数据库、Sharedpreferences
查看>>