admin管理员组文章数量:1023187
Is it possible to namespace a JavaScript file inserted dynamically?
I know that I can dynamically include a JavaScript file by creating a script
tag and insert it into the DOM, but can this included file be namespaced? So, if the file has a function called bar
, I would want access it through a namespace, say foo
: i.e. foo.bar()
.
Is it possible to namespace a JavaScript file inserted dynamically?
I know that I can dynamically include a JavaScript file by creating a script
tag and insert it into the DOM, but can this included file be namespaced? So, if the file has a function called bar
, I would want access it through a namespace, say foo
: i.e. foo.bar()
.
2 Answers
Reset to default 6Yes, CommonJS Modules/1.1 specifies only one way of doing it.
I've used it only with Node.js on server side, but I believe there are other libraries created to work with browser that are CommonJS pliant. Beware that there are multiple module specifications for server/browser (didn't dig into that yet).
Modules are written just like any other piece of javascript, the only addition is you export what you want to expose:
module.exports.bar = Bar;
function Bar() {
// code
}
And the usage:
var foo = require('mymodule');
foo.bar();
What is actually done in the background, the whole code is wrapped into another function and exports are its properties.
Also, Michael Bolin talked about similar problem in his talk about 'with' keyword at JSConf.
If you mean to add a namespace to everything that is defined in that file while loading it dynamically, without modifying the file itself, the answer is no.
Is it possible to namespace a JavaScript file inserted dynamically?
I know that I can dynamically include a JavaScript file by creating a script
tag and insert it into the DOM, but can this included file be namespaced? So, if the file has a function called bar
, I would want access it through a namespace, say foo
: i.e. foo.bar()
.
Is it possible to namespace a JavaScript file inserted dynamically?
I know that I can dynamically include a JavaScript file by creating a script
tag and insert it into the DOM, but can this included file be namespaced? So, if the file has a function called bar
, I would want access it through a namespace, say foo
: i.e. foo.bar()
.
2 Answers
Reset to default 6Yes, CommonJS Modules/1.1 specifies only one way of doing it.
I've used it only with Node.js on server side, but I believe there are other libraries created to work with browser that are CommonJS pliant. Beware that there are multiple module specifications for server/browser (didn't dig into that yet).
Modules are written just like any other piece of javascript, the only addition is you export what you want to expose:
module.exports.bar = Bar;
function Bar() {
// code
}
And the usage:
var foo = require('mymodule');
foo.bar();
What is actually done in the background, the whole code is wrapped into another function and exports are its properties.
Also, Michael Bolin talked about similar problem in his talk about 'with' keyword at JSConf.
If you mean to add a namespace to everything that is defined in that file while loading it dynamically, without modifying the file itself, the answer is no.
本文标签: Namespace a dynamically loaded javascript file39s contentsStack Overflow
版权声明:本文标题:Namespace a dynamically loaded javascript file's contents - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745543914a2155307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论