小编Dal*_*ung的帖子

在javascript中复制python的__call__?

我想复制实例化一个可调用的类而不使用模块模式.

以下是我最好的尝试.但是,它使用__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)

javascript prototype-programming

8
推荐指数
1
解决办法
1618
查看次数

标签 统计

javascript ×1

prototype-programming ×1