什么是JavaScript中self.self ==自检的目标是什么? [英] What is the goal of self.self == self check in JavaScript?

查看:280
本文介绍了什么是JavaScript中self.self ==自检的目标是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Backbone.js的开头:

  //建立在浏览器中根对象,`window`(`self`),或`global`在服务器上。
//我们使用的'self`代替`window`为`WebWorker`支​​持。
VAR根=(typeof运算自我=='对象'和;&安培; self.self ==自我和放大器;&安培;个体经营)||
           (typeof运算全球=='对象'和;&安培; global.global ==全球和放大器;&安培;全局);

什么是 self.self ==自的呢?什么时候能是假的?结果
同样关于 global.global ==全球


解决方案

 函数错误(X);
    自=这一点;
    x.on(事件,函数(){
        的console.log(个体经营);
    });
}
新的错误(...);

你发现了吗?现在,我们已经有了一个全球性的这是不是的 的骨干预期。因此,它会检查是否其实是全局对象,这很可能是这种情况,当是一个对象,对象具有全局变量为指向对象本身的属性。

同为全球

backbone.js starts with:

//Establish the root object, `window` (`self`) in the browser, or `global` on the server.  
//We use `self` instead of `window` for `WebWorker` support.  
var root = (typeof self == 'object' && self.self == self && self) ||  
           (typeof global == 'object' && global.global == global && global);  

What is self.self == self for? When can it be false?
Same about global.global == global.

解决方案

function Mistake(x);
    self = this;
    x.on("event", function() {
        console.log(self);
    });
}
new Mistake(…);

Did you spot it? Now we've got a global self that is not the self that backbone expects. So it checks whether self actually is the global object, which is likely to be the case when self is an object and the object has the "global variable" self as a property that points to the object itself.

Same for global.

这篇关于什么是JavaScript中self.self ==自检的目标是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆