简体   繁体   English

将属性/方法全局添加到对象?

[英]Adding properties/methods to an object globally?

I have been analyzing some javscript source codes and found that an object is declared slightly different that normal procedure and then how its used throught in the program.Its declared like this 我一直在分析一些javscript源代码,发现一个对象的声明与正常过程略有不同,然后在程序中如何使用它。

window.ext = {};

And its properties been added in common procedure like this 它的属性是在像这样的通用过程中添加的

ext.webRequest = {
    Property1/function2 : Property
    Property2/function2: function()
   }

My doubts are 我的怀疑是

  • Is it normal to omit window.ext.webrequest and simply call ext.webrequest ? 省略window.ext.webrequest并简单地调用ext.webrequest是否正常?
  • Is there any hidden reason/advantage to declare object like that ? 是否有任何隐藏的理由/优势来声明这样的对象?

Global variables are stored the global object (this is how its called according to ECMA script specification). 全局变量存储在全局对象中(这是根据ECMA脚本规范如何调用的)。

In most web browsers they call the global object window and can be accessed without specifying its name. 在大多数Web浏览器中,它们称为全局对象window ,无需指定其名称即可访问。

so eventually: 所以最终:

window.ext = {}; and ext = {}; ext = {}; are equivalent (if and only if you call them within the same global scope and not in a nested scope (ie a function or an object scope)). 等价的(当且仅当您在相同的全局范围内而不是在嵌套范围内(即函数或对象范围)调用它们时)。

Is there any hidden reason/advantage to declare object like that ? 是否有任何隐藏的理由/优势来声明这样的对象?

The advantage/reason is that declaring objects like that make them accessible at all levels since they will be declared in the global scope. 优点/原因是,像这样声明对象使它们可以在所有级别访问,因为它们将在全局范围内声明。

Is it normal to omit window.ext.webrequest and simply call ext.webrequest? 省略window.ext.webrequest并简单地调用ext.webrequest是否正常?

No , there are differences. ,有区别。

Window is a global object and also a host object. Window是一个全局对象,也是一个宿主对象。 If you are not giving a window context (or global) to a property like obj.ext.webrequest JS engine will 如果您没有为obj.ext.webrequest这样的属性提供窗口上下文(或全局),JS引擎将

  • First look for this object in the local context, then it parent and so one till the global context. 首先在本地上下文中查找此对象,然后在父对象中查找,直到全局上下文为止。 Context are defined at function level and not at block level. 上下文是在功能级别而非块级别定义的。

  • If the context is available locally, it will use that value and not window.ext.webrequest value. 如果上下文在本地可用,它将使用该值,而不使用window.ext.webrequest值。

Is there any hidden reason/advantage to declare object like that ? 是否有任何隐藏的理由/优势来声明这样的对象?

One advantage that I have observed is when you want to invoke a method from the onclick attribute like 我观察到的一个优点是,当您想从onclick属性中调用方法时,例如

<div onclick="method1()">Click here</div>

This will automatically look for this method in global context. 这将在全局上下文中自动查找此方法。

But usually we want to wait for the dom to be loaded (like in JS fiddle it is a default setting), and in that case your method definitions are wrapped inside document.onload event handler function like 但是通常我们要等待dom加载(就像在JS fiddle中那样是默认设置),在这种情况下,您的方法定义将包装在document.onload事件处理函数中,例如

document.onload = function(){

   var method1 = function ()
   { 
     //some action here
   }
};

Now this method1 function is not visible in a global context, so the onclick event will fail to find this method. 现在,此method1函数在全局上下文中不可见,因此onclick事件将找不到该方法。

But if you assign this method to a global context like this 但是如果您将此方法分配给这样的全局上下文

document.onload = function(){

   window.method1 = function ()
   { 
     //some action here
   }
};

Now, it will be available for onclick event. 现在,它将可用于onclick事件。

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

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