我可以在';以外的上下文中加载javascript文件吗;窗口';?

我可以在';以外的上下文中加载javascript文件吗;窗口';?,javascript,namespaces,resolution,identifier,Javascript,Namespaces,Resolution,Identifier,我试图加载一些外部的.js文件,并且有一些无法解决的名称空间冲突 我的想法是在自己的上下文中加载一些文件,将“this”从指向窗口对象替换为某个自定义名称空间 例如: first.js: name = "first"; second.js: name = "second"; 在我看来,这种把戏可能非常有用。有可能吗 编辑 似乎替换“this”并不能解决问题,因为它不是javascript中标识符解析的默认上下文。这是我的测试代码: var first = {}; var second = {

我试图加载一些外部的.js文件,并且有一些无法解决的名称空间冲突

我的想法是在自己的上下文中加载一些文件,将“
this
”从指向窗口对象替换为某个自定义名称空间

例如:

first.js:

name = "first";
second.js:

name = "second";
在我看来,这种把戏可能非常有用。有可能吗

编辑
似乎替换“
this
”并不能解决问题,因为它不是javascript中标识符解析的默认上下文。这是我的测试代码:

var first = {};
var second = {};

(function(){name = "first";}).call(first);
(function(){name = "second";}).call(second);


document.write('name= '+name+' <br/>\n'); //prints "second"
document.write('first.name= '+first.name+' <br/>\n'); //prints "undefined"
document.write('second.name= '+second.name+' <br/>\n'); //prints "undefined
var first={};
var second={};
(function(){name=“first”;}).call(first);
(function(){name=“second”;}).call(second);
document.write('name='+name+'
\n')//打印“第二” document.write('first.name='+first.name+'
\n')//打印“未定义” document.write('second.name='+second.name+'
\n')//打印“未定义”
有什么想法吗

分辨率
这是不可能的。我最终比今天早上更聪明,我放弃了。 我向任何有类似问题的人推荐这些启发性的阅读材料,他们可能想尝试一下:

您可以使用以下内容包装js文件的内容:

var externalInterfaceForYourObject = (function(){
  //code that defines your object

  //this should refer to the current anonymous function, and not the window object
  return this;
})();

您可以在
iframe
中加载文件,该文件不是
.js
,而是HTML文件,如:

<html>
<body>
  <script>
    var $ = parent.$, // you can share objects with the parent, eg: jQuery
      localObject = { // your local object definition
        name: 'first',
        showName: function(){
          $('div.name').html( this.name );
        }
      };
    //assign the local object to the custom namespace
    parent.customNamespace.object1 = localObject; 
  </script>
</body>
</html>

var$=parent.$,//您可以与父对象共享对象,例如:jQuery
localObject={//您的本地对象定义
姓名:'第一',
showName:function(){
$('div.name').html(this.name);
}
};
//将本地对象分配给自定义命名空间
parent.customNamespace.object1=localObject;

诀窍是使用
parent.
获取父页面中可用的javascript对象。

我不清楚您这样做的原因;您使用
这个
到底是为了什么

在匿名函数中包装second.js的内容将防止该文件中的变量与全局变量冲突。如果确实必须将
this
设置为非全局对象的特定对象,可以执行以下操作

var differentThis = {};
(function() {
    // Contents of second.js go here
}).call(differentThis);
更新


你不能随心所欲。你似乎想访问变量对象,当你在JavaScript中声明变量时,属性被添加到该对象中。在全局代码中,变量对象是全局对象,因此你可以访问它;在函数中,这是执行上下文的一个属性,无法访问direc

我的一个想法是,在不需要修改外部JavaScript文件的情况下,以AJAXy方式获取JavaScript文件的内容(取决于您如何操作),然后使用
新函数(code)
方式将其全部放在函数中,然后使用
方式初始化:

surrogateWindow = new new Function(jsCode)();

然后,
代理窗口
就是该代码的
这个
。我认为这个想法应该行得通。

对于您编写的代码,我认为您误解了JavaScript中类的一些工作方式。在Java中,您可以删除
这个。
,但在JavaScript中您不能。您始终需要有
这个。
。因此,您的代码变成:

var first = {};
var second = {};

(function(){this.name = "first";}).call(first);
(function(){this.name = "second";}).call(second);


document.write('name= '+name+' <br/>\n'); //prints "undefined"
document.write('first.name= '+first.name+' <br/>\n'); //prints "first"
document.write('second.name= '+second.name+' <br/>\n'); //prints "second"
var first={};
var second={};
(function(){this.name=“first”;}).call(first);
(function(){this.name=“second”;}).call(second);
document.write('name='+name+'
\n');//打印“未定义” document.write('first.name='+first.name+'
\n');//打印“first” document.write('second.name='+second.name+'
\n');//打印“second”
以一种更普通的类方式来做也很好。我不确定你的具体情况是什么,因为我看不到你所有的代码,所以你可能已经这样做了

function Something(name) {
    this.name = name;
}

var first = new Something("first");
var second = new Something("second");

document.write('name= '+name+' <br/>\n'); //prints "undefined"
document.write('first.name= '+first.name+' <br/>\n'); //prints "first"
document.write('second.name= '+second.name+' <br/>\n'); //prints "second"
函数某物(名称){
this.name=名称;
}
var first=新事物(“第一”);
var second=新事物(“第二”);
document.write('name='+name+'
\n');//打印“未定义” document.write('first.name='+first.name+'
\n');//打印“first” document.write('second.name='+second.name+'
\n');//打印“second”
尽管这是一个老问题,但这个答案可能仍然适用于某些人:

加载js文件时,它会自动获取窗口的上下文。这是不可能更改的。 然而,如果您试图避免正在加载的库之间的冲突,并且您无法控制这些库,并且它们没有内置的“无冲突”机制,那么有一个很好的技巧- 您可以将它们加载到无源iframe中。 这将使它们的上下文成为iframe的窗口,并且您仍然能够访问iframe,因为这里没有跨域问题


您可以看到使用此技术的示例。

我对其进行了测试,匿名函数在窗口上下文下运行,并返回窗口对象。这似乎是一个肮脏但可行的解决方案,但我不能使用它,原因是我在原始问题中没有指定-我的代码来自文档的域之外,因此我的IFrame wi我将无法访问父级的内部状态。我在我们的应用程序中大量使用它来清晰地分离3个代码:HTML、CSS和JS。我发现这是一种不太脏的包含浏览器端的方式。由于您是跨域的,我认为唯一的方法是让您的服务器获取JS文件,转换并缓存它们,将它们封装在通过作为上下文,并将其发送到浏览器,例如:
http://yourdomain.com/fetchjs?url=http://otherdomain.com/other.js&namespace=custom.object1
祝你好运。遗憾的是,服务器端也不是一个选项。当我写“肮脏但可行”时“我在考虑我必须回答的优先事项——额外的页面负载被认为是非常昂贵的。我同意这一点