this指向undefined uiapp_this为什么会为undefined?
一、前言
普通function定义的函数
‘运行环境’也是对象,this
指向运行时所在的对象。 如下:
如果一个函数在全局环境运行,this就指向顶层对象(浏览器中为window对象); 如果一个函数作为某个对象的方法运行,this就指向那个对象; 如果一个函数作为构造函数,this指向它的实例对象。
箭头函数
函数体内的this
对象,就是定义时所在的对象,而不是使用时所在的对象。
本来记住这几点已经可以了,this
最终找到是可能window
,但是undefined
是怎么又是怎么来的,本妹子下面将一步步分析。
二、问题点:undefined是怎么来的
综上所述,this指向运行时所在的对象或指向定义时所在的对象,但是这个对象可能最后找到是window
,但都不可能是undefined
,那么undefined
是怎么来的呢?
<html>
<script type="text/javascript">
var foo = function foo() {console.log(this);
};
var foo1 = () => {console.log(this);
};
foo(); //Window
foo1(); //Window
</script>
</html>
三、回答
我们一般写js文件都是babel
转成ES6
的,babel
会自动给js文件上加上严格模式。
用了严格模式"use strict",严格模式下无法再意外创建全局变量,所以this
不为window
而为undefined
<html>
<script type="text/javascript">
"use strict";
var foo = function foo(){console.log(this)
};
foo();//undefined
</script>
</html>
四、进阶问题:严格模式对箭头函数没有效果
严格模式为什么对箭头函数没有效果,返回还是window
?
<html>
<script type="text/javascript">
"use strict";
var foo = () => {console.log(this)
};
foo(); //Window
</script>
</html>
五、进阶问题回答
Given that this comes from the surrounding lexical context, strict mode rules with regard to this are ignored.
lexical means that this refers to the this value of a lexically enclosing function.
综上所述,在箭头函数中,this
为lexical
类型,lexical
意味着这个this
指是所在封闭函数中this
,所以严格模式会自动忽视use strict
,所以this
如下所示:
<html>
<script type="text/javascript">
var foo = () => {"use strict";console.log(this)
};
foo(); //Window
</script>
</html>
箭头函数中,this
指向运行时所在的对象,而use strict
被移到函数内了,所以this
为全局变量window
。
Happy coding ~~ ^ ^
相关链接
this为什么会为undefined?blog.frontendx.cn
严格模式 - JavaScript
Arrow functions - JavaScript
ECMAScript 2015 Language Specification – ECMA-262 6th Edition
函数的扩展 - ECMAScript 6入门
use strict in javascript not working for fat arrow? - Stack Overflow
this指向undefined uiapp_this为什么会为undefined?相关推荐
- undefined symbol nvic 报错 undefined symbol TIM_Cmd报错
目录 undefined symbol nvic 报错 undefined symbol TIM_Cmd报错 undefined symbol nvic 报错 原因是在FWLB文件夹中未加入misc. ...
- php notice undefined variable,PHP提示Notice: Undefined variable错误的解决办法
在调试程序的时候,会出现 Undefined variable错误,例如: Notice: Undefined variable: insert in - Notice: Undefined vari ...
- php undefined empty,怎样处理php undefined index毛病问题_后端开发
php index毛病的处置惩罚办法:1.修正php.ini中的error设置下的毛病显现体式格局:2.对变量举行初始化:3.举行"isset($_post['']),empty($_pos ...
- php post undefined index,PHP 中提示undefined index如何解决(多种方法)
一.相关信息 平时用$_post['']或$_get['']获取表单中参数时会出现Notice: Undefined index: --------: 以及我们经常接收表单POST过来的数据时报Und ...
- uni-app 父组件无法获取到子组件传来的值,为undefined;父子组件传值undefined
一开始写的 <uni-modle ref="uniModle" @handleButton="handleButton()"></uni-mo ...
- php notice undefined variable,PHP提示Notice: Undefined variable的解决办法
PHP默认配置会报这个错误,我的PHP版本是5.2.13,存在这个问题: Notice: Undefined variable 这就是将警告在页面上打印出来,虽然这是有利于暴露问题,但实现使用中会存在 ...
- JavaScript:undefined And null差异
班吃饭的时候,同事偶然问了一个问题:undefined和null究竟有什么差别?无法回答,回去查阅相关文档,算了有了一个了解,做相关的总结.在開始之前,请看例如以下代码,算是抛出这个问题: conso ...
- 理解Javascript_02_理解undefined和null
来自普遍的回答: 其实在 ECMAScript 的原始类型中,是有Undefined 和 Null 类型的. 这两种类型都分别对应了属于自己的唯一专用值,即undefined 和 null. 值 un ...
- null、undefined、NaN区分解析和条件判定,以及在IF条件中的判定
NaN的理解和用法: NaN 属性是代表非数字值的特殊值.该属性用于指示某个值不是数字.可以把 Number 对象设置为该值,来指示其不是数字值. Number.NaN 是一个特殊值,说明某些算术运算 ...
最新文章
- 关于feign调用时,session丢失的解决方案
- 成功解决 class 'AttributeError' : 'Editor' object has no attribute '_Editor__markerMap'
- 将数据、代码、栈放入不同的段
- 操作系统(十七)调度算法(二)
- python在线翻译脚本_用python实现百度翻译的示例代码
- 将自己的类封装为lib的方法
- java rmi 入门实例
- 如何获取租户中所有的Team
- 正则表达式 python_Python正则表达式总结
- 为5—18岁青少年提供营地教育,漫族完成百万级天使轮融资
- C语言 5个数最值问题
- EPLAN P8.2.7 学习版安装教程(适用于 Win10 64位)
- QGIS的部分使用流程
- 全栈必备 存储基础
- CSS学习笔记 01、CSS3基础知识学习
- 【华为机试真题Python】工厂流水线调度
- Whitelabel Error Page 的原因
- 关于要走的游戏之路,一点点小心思
- ubuntu18.04 输入法的配置
- 小程序源码:强大多流量主自带接口短视频去水印工具箱微信小程序
热门文章
- hadoop免密钥配置
- 用撸Arduino的方法撸STM32F103xx
- graphpad如何检测方差齐_如何选择方差分析中“多重比较”的方法?
- Error:Internal error: org.gradle.tooling.BuildException: Could not run build action using Gradle dis
- HTML与CSS基础之子和后代元素选择器(八)
- Swift--变量和常量
- php7中使用 xhprof 分析
- 1047 行 MySQL 详细学习笔记(值得学习与收藏)
- 阿里安全猎户座实验室(Alibaba Orion Security Lab)简介
- C++ decltype类型说明符(尾置返回类型使用)