javascript - 'this' 和普通变量声明之间的区别
问题描述
this
在我的函数中声明变量与通常使用let
or声明有什么区别var
?
const controller = (function() {
this.name = 'John';
})();
const controller2 = (function() {
let name = 'Mary';
})();
解决方案
this
你的函数里面是window
对象。因此,您不是在创建变量,而是在向窗口对象添加属性
console.log((function(){return this})() === window)
推荐阅读
- r - 给定 R 中的条件,查找与另一个值最接近的值的位置
- node.js - 使用 Node js 按周/月/年对文档进行分组
- javascript - React 输入 defaultValue 重新渲染
- flutter - 视频播放器中的颤振问题
- bash - Bash:使用 getopts 解析参数后的选项
- django - 分配前引用的 Django UnboundLocalError 局部变量“注册”
- powershell - Powershell 图形用户界面。从动态列表框中选择一个项目,随后从 Active Directory 中卸载用户信息
- c# - 如何在 WinForms 按钮的文本之前添加图像图标?
- docker - Docker多阶段不调用入口点
- wordpress - 收到一定数量的视图后如何在 WordPress 上隐藏内容