我想在不使用模块模式的情况下复制实例化可调用类。
以下是我对此的最佳尝试。但是,它使用__proto__
我不确定。这可以在没有 的情况下完成__proto__
吗?
function classcallable(cls) {
/*
* Replicate the __call__ magic method of python and let class instances
* be callable.
*/
var new_cls = function () {
var obj = Object.create(cls.prototype);
// create callable
// we use func.__call__ because call might be defined in
// init which hasn't been called yet.
var func = function () {
return func.__call__.apply(func, arguments);
};
func.__proto__ = obj;
// apply init late so it is bound to func and not cls
cls.apply(func, arguments);
return func;
}
new_cls.prototype = cls.prototype;
return new_cls
}