使用 MooTools 的匿名类
我可以使用 MooTools js 框架创建匿名类(java 术语)吗? 假设它应该看起来像:
A = new Class({
invoke: function() {
alert('class');
}
});
a = new A({
invoke: function() {
this.parent();
alert('instance');
},
});
但我不想修改构造函数接口来接受其他属性。 是否有更舒适的方法来定义不多次使用的类?
Can I create the anonymous class(java term) using the MooTools js framework?
Suppose it should look like:
A = new Class({
invoke: function() {
alert('class');
}
});
a = new A({
invoke: function() {
this.parent();
alert('instance');
},
});
But I don't want to modify constructor interface around to accept additional properties.
Is there more comfortable way to define classes that are used not more than once?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只能在要使用该类的范围内定义该类,这意味着该类在该范围之外将不可用。例如,使用自执行函数:(
我修复了代码中的几个错误,包括在声明
MyClass
和myObject< 时省略
var
关键字/code>,以及初始化myObject
时额外的,
。)You could define the class only within the scope it is going to be used in, which means the class won't be available outside that scope. For example, using a self-executing function:
(I've fixed up several errors in your code, including omitting the
var
keyword when declaringMyClass
andmyObject
, and an extra,
when initialisingmyObject
.)