简体   繁体   English

带有回调的Javascript函数,该回调将已初始化的对象作为参数传递

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

I am trying to extend some existing functionality so it has a callback function that passes the original object as a parameter, or at least any public variables that is stored in a smaller object, how can I achieve this? 我正在尝试扩展一些现有功能,以便它具有一个回调函数,该回调函数将原始对象作为参数传递,或者至少将存储在较小对象中的任何公共变量作为参数传递,我该如何实现?

This is a simplified version of the file I am working with, a class would be contained in a variable 这是我正在使用的文件的简化版本,一个类将包含在变量中

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

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

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

The function / class looks similar to this 函数/类看起来与此类似

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();
            }
        }
    })();
}

Thanks 谢谢

(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);

Check this out :) 看一下这个 :)

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);
    }
});

Live demo: http://jsfiddle.net/P3bzM/ 现场演示: 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_);
            }
        }
    })();
}

Look for word "total" to see the changes. 查找单词“ total”以查看更改。

You've got several ways to achieve this. 您有几种方法可以实现这一目标。

  1. Simply add parameters when calling your callback: 只需在调用回调时添加参数:

     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. Use .call() or .apply() javascript method to set the context of the callback (what will be this within the callback) 使用.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); } 

    Both methods works the same way, they simply differ in the way you pass arguments to the method. 两种方法的工作方式相同,只是将参数传递给方法的方式不同。 Read this to get more info 阅读以获得更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM