我正在使用 ExtJs 5 和 SenchaCmd。
在一个包中,我在几个文件中定义了几个类,如下所示:
// Bar1.js
Ext.define('Foo.Bar1', {
...
});
// Bar2.js
Ext.define('Foo.Bar2', {
...
});
现在我想用一些像这样的通用工具来“增强”Foo 命名空间:
Ext.ns('Foo');
Foo.tool1 = function() { ... }
Foo.tool2 = function() { .... }
Foo.generalProp1 = 42;
(...)
什么是更好的地方和实践来声明这个以便 Sencha 编译器也嵌入这个文件?
是否有可能(在某种程度上)像我们需要一个类一样“需要”一个命名空间?
最佳答案
您可以使用新分机中的statics
:
Ext.define('Foo', {
// declare static members
statics: {
method: function(){ return "static method"; }
}
});
// Define class under Foo namespace
Ext.define('Foo.OtherClass', {
method: function(){ return "instance method"; }
});
// Create instance of Foo.OtherClass
var o = Ext.create('Foo.OtherClass');
// Use static member
console.log(Foo.method());
// Use instance member
console.log(o.method());
然后您可以将 Foo
命名空间视为类,并将其放置为任何其他类。
那么显然您也可以要求该 namespace ,因为它也是类。
关于extjs - 使用 ExtJs5 定义命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27106193/