0
// util/model.js

define(['util/class'], function(classCtor) {
    return classCtor.create({
        set: function(key, value) {
            // this doesn't work, because self is pointing to window
            if (value instanceof self) {
                // do Something
            }
        }
    });
});

这应该是我所有实体的基类。因为我的模块是用 RequireJS 风格编写的,所以我可以使用以下解决方法:

if (value instanceof require('util/model'))

这工作正常,但现在我的代码耦合到 ModuleName。有什么建议可以避免这种耦合吗?顺便说一句:我使用来自Steffen Rusitschka的基于原型的助手来构建我的类

4

1 回答 1

0

你可以只为你的匿名类使用一个变量:

define(['util/class'], function(classCtor) {
    var self = classCtor.create({
        set: function(key, value) {
            // self is the class now:
            if (value instanceof self) {
                // do Something
            }
        }
    });
    return self;
});
于 2013-01-29T16:57:03.130 回答