一般认为:严格模式下this不允许指向全局对象。如 > 需要说明的是:本身指向全局的this是没有问题的。示例代码:<!DOCTYPE html><html lang="zh"> <head> <meta ...
一般认为:严格模式下this不允许指向全局对象。
如 >
需要说明的是:本身指向全局的this是没有问题的。
示例代码:
<!DOCTYPE html><html lang="zh"> <head> <meta charset="UTF-8" /> <title>严格模式下this指向</title> </head> <body> <script type="text/javascript"> 'use strict'; console.log(this); </script> </body></html>
控制台输出为window对象(全局对象):
严格模式下this不允许指向全局对象是指在函数内部,如下示例代码:
<!DOCTYPE html><html lang="zh"> <head> <meta charset="UTF-8" /> <title>严格模式下this指向</title> </head> <body> <script type="text/javascript"> 'use strict'; function F() { this.a = 1; //这种指向全局的this不对 }; F(); </script> </body></html>
控制台输出报错:
原标题:JavaScript严格模式下this指向
关键词:JavaScript
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。