我想复制实例化一个可调用的类而不使用模块模式.
以下是我最好的尝试.但是,它使用__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 …Run Code Online (Sandbox Code Playgroud)