Default function on an object?

后端 未结 1 727

Is it possible to set a default function on an object, such that when I call myObj() that function is executed? Let\'s say I have the following func ob

相关标签:
1条回答
  • 2021-02-06 09:46

    return a function:

    function func(_func) {
        this._func = _func;
    
        return function() {
            alert("called a function");
            this._func();
        }
    }
    
    var test = new func(function() {
        // do something
    });
    
    test();
    

    but then this refers to the returned function (right?) or window, you will have to cache this to access it from inside the function (this._func();)

    function func(_func) {
        var that = this;
    
        this._func = _func;
    
        return function() {
            alert("called a function");
            that._func();
        }
    }
    
    0 讨论(0)
提交回复
热议问题