命名空间QScriptEngine扩展
Namespacing QScriptEngine extensions
是否有人向我解释如何在QScriptEngine扩展中获得命名空间?我已经设置了它,所以我在script/下有一个目录foo/,其中__init__.js文件正在执行。
__setupPackage__(__extension__);
print(__extension__);
hello = function() { return 5; };
在我的c++代码中:engine.evaluate (" hello (); ");//运行正常
所以我的问题是,像foo/bar/whatever (foo.bar.whatever)这样的文件层次结构的意义是什么,如果它们都集中到一个全局命名空间中?我看到过一些例子,他们试图在代码中创建一个命名空间,但我似乎无法在没有编译器错误的情况下工作。
foo = {
hello : function() { return 5; }
};
在我的c++代码中:
engine.evaluate("foo.hello();");
我误解了Qt处理名称空间的方式吗?是否应该将所有内容都放入全局作用域,而不管它是从哪个文件中获取的?是否有创建这些类型的名称空间的适当示例?谢谢。
JavaScript中不存在命名空间(如c++中所知)。在JS中获得类似名称空间的行为的唯一方法是将内容封装在对象中,对象的名称定义了名称空间
这个例子将在JS中创建一个'命名空间'foo,包含一个方法'bar'和一个命名空间'foo。包含方法'hello':
var foo = {
bar : function(){ return 5; },
baz : {
hello : function(who){ return 'Hello ' + who + '!'; }
}
};
在JavaScript中,我们通过使用闭包来模拟名称空间。这对于在命名空间中隐藏某些不想向用户开放的函数和属性也很有用
(function(window, undefined){
//declare a local object
myNamespace = {};
//define private variables
var privateVar; // this wont be accessable outside of the closure
var privatefunct = function () {
alert('I can only be called by functions defined within the closure');
}
myNamespace.publicVar = "this can be accessed outside the namespace";
// this function can be called outside the closure
myNamespace.getPrivateVar = function() {
return privateVar;
}
//add your local object to the global object (aka. window in the browser)
window.myNamespace = myNamespace;
})(window)
这个模式部分借用了jQuery,但它有几个优点,因为它可以保护你的代码免受恶意代码,可能会试图改变window的值或undefined
我不确定这是否完全回答了你的问题,但我希望它有帮助!
如果QScriptEngine是JavaScript实现,则
var foo = {
hello : function() { return 5; }
};
或
var foo = {};
foo.hello = function() { return 5; };
必须正常工作。否则,提供您得到的语法错误的确切文本。
如果你需要命名空间,你可以考虑我的TIScript: http://www.codeproject.com/KB/recipes/TIScript.aspx
- Node.js v6.2.0类扩展不是函数错误
- 扩展移相器按钮类不工作
- chrome扩展中的navigator.geolocation.getCurrentPosition
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- chrome扩展更改主机/域警告
- TableExport jquery插件:文件名和扩展名问题
- 如何在chrome扩展中重定向到html页面
- Chrome扩展没有't在重新加载之前考虑期权价值
- 扩展SVGTextElement时出现Typescript Uncaught TypeError
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- Javascript”;类“;带有参数的扩展
- 试图阻止Chrome通过扩展关闭
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 表达式引擎扩展开发-向呈现的条目添加自定义javascript
- 从选项页面更新chrome扩展清单权限
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- Chrome扩展:遍历不同的页面并收集数据
- DataTable按下键选择扩展/导航
- 扩展jQuery插件以更改配置
- 命名空间QScriptEngine扩展