簡體   English   中英

帶有回調的Javascript函數,該回調將已初始化的對象作為參數傳遞

[英]Javascript function with a callback that passes the initiated object as a parameter

我正在嘗試擴展一些現有功能,以便它具有一個回調函數,該回調函數將原始對象作為參數傳遞,或者至少將存儲在較小對象中的任何公共變量作為參數傳遞,我該如何實現?

這是我正在使用的文件的簡化版本,一個類將包含在變量中

<html>
<head>
</head>
<script>

var foo = new Foo ({
    arg1:1,
    onComplete: function(){
        alert("complete " + total);
    }
});

</script>
<body>
</body>
</html>

函數/類看起來與此類似

function Foo(options) {
    // private
    var number1_ = options.arg1;
    var number2_ = 10;
    var total_ = number1_ + number2_;

    // public
    this.total = total_;

    (function Foo() {
        if (options) {
            if (options.onComplete && typeof (options.onComplete) === "function") {

                // how do I pass Foo or an object of selected variables here?
                options.onComplete();
            }
        }
    })();
}

謝謝

(function Magazine() {
    if (options) {
        if (options.onComplete && typeof (options.onComplete) === "function") {

            // if you want `this === the foo instance` in the function:
            options.onComplete.call(this);
            // otherwise just pass it as an argument:
            options.onComplete(this);
        }
    }
}).call(this);

看一下這個 :)

function Foo(options) {
    // private
    var number1_ = options.arg1,
        number2_ = 10,
        total_ = number1_ + number2_;
    // public
    this.total = total_;

    (function Magazine( that ) {

        (options.onComplete || function(){}).call( that );

    })(this);
}

var foo = new Foo({
    arg1: 1,
    onComplete: function() {

        alert("complete " + this.total);
    }
});

現場演示: http//jsfiddle.net/P3bzM/

<html>
<head>
</head>
<script>

var foo = new Foo ({
    arg1:1,
    onComplete: function(total){
        alert("complete " + total);
    }
});

</script>
<body>
</body>
</html>


function Foo(options) {
    // private
    var number1_ = options.arg1;
    var number2_ = 10;
    var total_ = number1_ + number2_;

    // public
    this.total = total_;

    (function Magazine() {
        if (options) {
            if (options.onComplete && typeof (options.onComplete) === "function") {

                // how do I pass Foo or an object of selected variables here?
                options.onComplete(total_);
            }
        }
    })();
}

查找單詞“ total”以查看更改。

您有幾種方法可以實現這一目標。

  1. 只需在調用回調時添加參數:

     if (options.onComplete && typeof (options.onComplete) === "function") { // how do I pass Foo or an object of selected variables here? options.onComplete(this.number1_, this.number2_, this.total_); } 
  2. 使用.CALL()或。適用()JavaScript方法來設置回調的情況下(這將是this回調中)

     if (options.onComplete && typeof (options.onComplete) === "function") { // how do I pass Foo or an object of selected variables here? options.onComplete.apply(this); // options.onComplete.call(this); } 

    兩種方法的工作方式相同,只是將參數傳遞給方法的方式不同。 閱讀以獲得更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM