Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中引用要扩充的对象_Javascript_Oop - Fatal编程技术网

在javascript中引用要扩充的对象

在javascript中引用要扩充的对象,javascript,oop,Javascript,Oop,我正在尝试将所有内容本地化为javascript中的名称空间。因此,我有遵循命名约定的对象,如: myapp.utilities.file.spinner 等等 我的问题是,有没有办法避免每次我想用属性或方法扩充对象时重复那个大字符串。目前我的代码看起来像这样 myapp.utilities.file.spinner.method1 = function() { }; myapp.utilities.file.spinner.method2 = function() { }; etc. spi

我正在尝试将所有内容本地化为javascript中的名称空间。因此,我有遵循命名约定的对象,如:

myapp.utilities.file.spinner

等等

我的问题是,有没有办法避免每次我想用属性或方法扩充对象时重复那个大字符串。目前我的代码看起来像这样

myapp.utilities.file.spinner.method1 = function() { };
myapp.utilities.file.spinner.method2 = function() { };
etc.
spinnerPath.method1 = function()
像这样的

myapp.utilities.file.spinner.method1 = function() { };
myapp.utilities.file.spinner.method2 = function() { };
etc.
spinnerPath.method1 = function()
…其中
spinnerPath
代表
myapp.utilities.file.spinner
,会更好。但根据我的理解,我不能只说

spinnerPath = myapp.utilities.file.spinner
因为这将在全局空间中创建另一个对象

谢谢

试试这个:

(function(){
  var spinner = myapp.utilities.file.spinner;
  spinner.method1 = function(){};

})();

您只需将临时局部变量包装在匿名函数中即可:

(function(){
  var spinnerPath = myapp.utilities.file.spinner;
  spinnerPath.method1 = function() { };
  spinnerPath.method2 = function() { };
  spinnerPath.method1();
})();
(function (S)  
{  
   S.method1 = function(){/*whatever*/};  
   S.method2 = function(){/*whatever*/};  
})(myapp.utilities.file.spinner)

这里,
spinnerPath
实际上是全局
myapp.utilities.file.spinner
对象的本地引用,而不是副本。JavaScript中的对象是引用,因此如果创建指向它的局部变量,则不会创建副本或污染全局命名空间。

您使用的代码实际上不会创建新对象,而只是创建引用现有对象的新全局变量。但是,它会污染全局名称空间,因此如果您希望避免这种情况,可以选择以下几种方法:

myapp.utilities.file.spinner.method1 = function() { };
myapp.utilities.file.spinner.method2 = function() { };

...

// Somewhere else in your code, create a temp local called "spinner" 
// that references your longer path object.
var spinner = myapp.utilities.file.spinner;
spinner.method1();
  • 您可以将
    一起使用,但不要这样做,因为这可能会让您心痛不已

  • 您可以在全局命名空间之外的每个函数中创建一个速记指针变量:
    var s=myapp.utilities.file.spinner,但这很烦人

  • (可能是最好的选择)使用立即调用函数创建“私有命名空间”:

    (function(){
      var spinnerPath = myapp.utilities.file.spinner;
      spinnerPath.method1 = function() { };
      spinnerPath.method2 = function() { };
      spinnerPath.method1();
    })();
    
    (function (S)  
    {  
       S.method1 = function(){/*whatever*/};  
       S.method2 = function(){/*whatever*/};  
    })(myapp.utilities.file.spinner)
    

此JavaScript是在浏览器中运行还是在服务器端运行?@musicfreak。但只有当新变量是函数的局部变量时才是这样吗?如果OP想要全局引用
喷丝头路径
,但作为引用,该怎么办?不是引用,抱歉。如果OP想要在全局范围内使用
spinnerPath
该怎么办?没关系,在这种情况下,您可以去掉匿名函数;但我理解这个问题的方式是,他不想污染全局名称空间。我最喜欢这个名称空间,但老实说,它对我来说没有任何意义。你能把它的背景再具体一点吗?只要显示OP在哪里可以使用速记。@Anthony:他可以在任何地方使用速记。它所做的只是创建一个匿名函数并立即调用它,将对象引用作为参数传递-当然,参数名只在函数本身的作用域中。Shog9是正确的-将它放在任何位置(在OP的情况下,可能在全局作用域中)。例如,这是制作jQuery插件的首选方法,而不必到处键入“jQuery”(同时仍然尊重jQuery的
noConflict
选项)。这是一个内存整洁的解决方案。我会爱上你的!