简体   繁体   English

Javascript:“顶级 'this' 表达式”

[英]Javascript: "Top-level 'this' expression"

I get the following warning/hint/error when I have an object like this:当我有这样的对象时,我收到以下警告/提示/错误:

(
    function global(){...};
    function moreFunctions(){...};
)(this);

"Top-level 'this' expression. This inspection reports instances of Javascript 'this' expression occuring outside of object literals or constructor bodies. Such this expressions are legal Javascript, and reference the top-level "global" Javascript object, but are largely useless." “顶级 'this' 表达式。此检查报告在对象字面量或构造函数体之外发生的 Javascript 'this' 表达式的实例。这样的 this 表达式是合法的 Javascript,并引用顶级“全局”Javascript 对象,但主要是无用。” (by InspectionJS) (由检查JS)

By the way, jQuery has the same with (window) instead of (this) .顺便说一句,jQuery 与(window)而不是(this)相同。

I don't understand what it means.我不明白这是什么意思。 All I know is that everthing between the first ( and second ) is an object, but what's that addition?我所知道的是第一个(和第二个)之间的一切都是一个对象,但是那个添加是什么?

I've got into this because I've just discovered a JS library source and somehow when it is included in my existing scripts everything just stops working.我进入这个是因为我刚刚发现了一个 JS 库,当它包含在我现有的脚本中时,一切都停止了工作。 When I deleted that (this);当我删除那个(this); part it didn't crash the page;部分它没有使页面崩溃; but the library just didn't work.但图书馆只是没有工作。

I'm not 100% sure on the problem you're having but let me explain what I learned from Paul Irish's video on lessons from the JQuery source. 我对你遇到的问题并不是100%肯定,但让我解释一下我从Paul Irish的视频中学到的关于JQuery源代码的经验教训。

(function(window, undefined) {
})(this);

This is known as a self-executing function. 这被称为自执行功能。 The function definition is put in brackets. 函数定义放在括号中。 (anything can be put in brackets in Javascript almost all the time). (几乎所有时间都可以将任何东西放在Javascript括号中)。 Then, the second () immediately calls the function. 然后,第二个()立即调用该函数。

so it's like doing the following 所以这就像做以下几点

function my_func(window, undefined){...}
my_func(this);

Now to explain the this. 现在解释一下this. Usually, we wrap our entire program in such a self-executing function. 通常,我们将整个程序包装在这样一个自动执行的函数中。 The this while calling, and the window and undefined are pretty much just fixes for edge-cases when sharing the code space. this在调用时,窗口和undefined几乎只是在共享代码空间时修复了边缘情况。 For example, someone could put something like 例如,有人可以提出类似的东西

window = 0;
//or
undefined = 1;

suddenly, these very important global variables we rely on break. 突然间,这些非常重要的全球变量依赖于突破。 using this at the top level returns the window variable to the inner function. 在顶层使用它将窗口变量返回到内部函数。 And since we don't pass any second variable to the function, undefined is back to its correct value. 由于我们没有将任何第二个变量传递给函数,因此undefined将返回其正确的值。

Hope all this helped. 希望这一切都有帮助。

It's complaining that you're using this outside of any function. 它抱怨你在任何功能之外使用this

Inside a function, the use of this (generally) means that the programmer intended the function as a method (where this is the object that the method is called on), but outside a function, it's just a strange thing to do. 在函数内部,使用this (通常)意味着程序员将函数作为一个方法( this是调用该方法的对象),但在函数外部,这只是一件奇怪的事情。

It's better to write window instead, to explicitly refer to the global object, rather than to rely on the fact that this implicitly refers to the global object when it's not used in a method call. 这是更好地写window代替,明确提及全局对象,而不是依靠事实, this暗示是指全局对象时,它不是在方法调用中使用。

This puts a whole chunk of code into a immediately executing function with its own scope. 这将整个代码块放入具有自己范围的立即执行的函数中。

For example: 例如:

(function(w){
    var apple = 'apple';
})(window);

alert(apple); //undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM