简体   繁体   English

将函数传递给Deferred的构造函数的优缺点

[英]Advantages/Disadvantages of Passing a Function into Deferred's Constructor

In playing with Deferred's I see many different combinations of use. 在玩Deferred的游戏时,我看到了许多不同的用法组合。 Most are the same, but occasionally I see ones that differ slightly. 多数是相同的,但偶尔会看到一些细微的差别。

FOR EXAMPLE... 例如...

NORMALLY, I SEE THIS : 通常,我看到以下内容
Here we are merely using the Deferred. 在这里,我们仅使用Deferred。

// NORMALLY I SEE THIS...
function doSomething(){

    var deferred = $.Deferred();
    var myClass = new MyClass();

    myClass.doSomething(function () {
        deferred.resolve();
    });

    return deferred.promise();
};

OCCASSIONALLY, I SEE THIS: 我偶尔会这样:
Here we are passing a function into the Deferred's constructor...then using it. 在这里,我们将一个函数传递给Deferred的构造函数...然后使用它。

// SOMETIMES I SEE THIS...
function doSomething() {

    var deferred = $.Deferred(function (deferred) {

        //Q: Is there an advantage to doing this instead?
        //Q: Is this a mis-use?
        var myClass = new MyClass();

        myClass.doSomething(function () {
            deferred.resolve();
        });
    });

    return deferred.promise();
};

MY QUESTION IS: 我的问题是:

  • Is there an advantage to doing the 2nd one instead? 代替做第二个有好处吗?
  • Is this a mis-use of the constructor? 这是对构造函数的滥用吗?
  • Does this practice create issue I just haven't seen yet? 这种做法是否会引起我尚未见过的问题?

I have yet to see any issue's arise from method 2. So, I'm looking for real insight here. 我还没有看到方法2引起的任何问题。因此,我正在这里寻找真正的见识。

Is there an advantage to doing the 2nd one instead? 代替做第二个有好处吗?

No, unless you enjoy callbacks which defeats the purpose of promises. 不可以,除非您喜欢回调而无法兑现承诺的目的。


Is this a mis-use of the constructor? 这是对构造函数的滥用吗?

Nope. 不。 jQuery deffered doc jQuery的文档

jQuery.Deferred( [beforeStart ] ) jQuery.Deferred([beforeStart])

 beforeStart Type: Function( Deferred deferred ) A function that is called just before the constructor returns. 

Does this practice create issue I just haven't seen yet? 这种做法是否会引起我尚未见过的问题?

No it doesn't, unless you intended to use myClass somewhere else which won't be possible since it is defined within your callback. 不,不是这样,除非您打算在其他地方使用myClass ,因为在回调中定义了myClass是不可能的。

Conclusion: 结论:

In the end, it's more of a personal preference. 最后,这更多是个人喜好。 Solution 1 just seems more clean to be honest. 老实说,解决方案1看起来更干净。

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

相关问题 在Javascript中调用函数时使用“()”的优点和缺点? - Advantages and disadvantages of using “()” while calling function in Javascript? 在 JavaScript 函数开始或结束时防止默认事件动作的优缺点 - Advantages and disadvantages of preventing default event action at start or end of JavaScript function 使用Javascript生成的网站的优点/缺点 - Advantages / Disadvantages to websites generated with Javascript 在 ES6 中创建带箭头或不带箭头的顶级函数有哪些优点/缺点? - What are the advantages/disadvantages for creating a top level function in ES6 with arrows or without? 将函数传递给javascript构造函数 - Passing function into javascript constructor Javascript的reduce()函数有哪些优点? (和map()) - What are the advantages of Javascript's reduce() function? (and map()) 结合使用jQuery UI和AngularJS的优缺点是什么? - What are the advantages and disadvantages of using Jquery UI with AngularJS? iPhone:使用CSS / JavaScript开发的优点和缺点是什么? - iPhone: What are the Advantages and Disadvantages of Developing with CSS/Javascript? JQuery和Glow JavaScript库的优点和缺点是什么? - What are the Advantages and Disadvantages of JQuery and Glow JavaScript Libraries? 这两种JavaScript模式之间的优缺点是什么? - What are the Advantages and Disadvantages Between These Two JavaScript Patterns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM