English 中文(简体)
职能反馈结构的最佳做法是什么? [闭门]
原标题:What s the best practice for function callback structure? [closed]

哪些职能反馈结构最能用于贾瓦特,为什么? 我看到这两种选择都使用了许多。 是否有其他东西?


Option A:

// DECLARATION
function funcA(required, success, error, options){

  // if there is an error in your function return 
  // and run error function
  if(errorHappens){ return error( an error ) };

  // if no error happened: run the success function
  success( all good here is your var back , required);
}

// USAGE
funcA( this is required , function(result){

  // all good, do something with the result :)

},
function(error){

  // tell the user about the error
  alert( error happened )  
},{
  optionalThing:  :-) 
});


Option B:

// DECLARATION
function funcB(required, callback, options){

  // if an error happens run the single callback with
  // the err param populated and return
  if(errorHappens){ return callback( an error ) };

  // if no error happened: run the callback with  null 
  // as the error param.
  callback(null,  this is a result );
}

.

// USAGE
funcB( this is required , function(err, result){
  if(err){ return alert( error happened ) };

  // all good do something with the result :)

},{
  optionalThing:  :-) 
}
最佳回答

这取决于环境。 j)

I would personally recommend you stick to callback as the last parameter

function doSomething(data, options, cb) {
  ...
  if (err) return cb(err);
  cb(null, result);
}

doSomething(data, { ... }, function (err, data) {
    if (err) throw err;
    ...
});

这主要是因为这种 co式习惯在de社区使用。 如果你不与点子互动。 j 毕竟,你或许应该使用你与我们重新互动的环境最常见的风格。

如果你的环境主要围绕 j,那么我就只回下一张<条码>美元。 递延从<条码>中删除

function doSomething(data, options) {
  return $.Deferred();
}

$.when(
  doSomething(data, { ... })
).then(
  function () { /* success */ },
  function () { /* error */ }
);

Edit:

function doSomething(data, options, cb) {
  if (typeof options === "function") {
    cb = options;
    options = null;
  }
  ...
}
问题回答

It doesn t really matter what you pick. I d prefer B because I can add more error codes without changing the function definition.

You can also use the another pattern where your callback is a hash of functions {error: function(){}, success: function(){}}. And this is extensible and quite common in JS libraries.

我倾向于通过字面字面字面,即:

var callback = {
    success : function(){},
    error : function(){},
    complete : function(){}
};

以这种方式使用:

function(callback){
    try{
        /*...processing...*/
        if(success)
            typeof callback.success ===  function  && callback.success(result);
        else 
            throw new Error( unsuccesfull );
    }catch(err){
        typeof callback.error ===  function  && callback.error(err.message);
    }finally{
        typeof callback.complete ===  function  && callback.complete() || typeof callback ===  function  && callback();
    }
}

主要好处是,以这种方式,我可以对每一种结果状况发出呼吁,但我也可以放弃我所希望的任何地位倒退。 我甚至可以仅通过一个功能(而不是一个物体),该功能相当于<代码>complete。

也没有。

我更喜欢一个充满论点的全称版本,而采用“共同”观点的简短方法作为参数。

最有意义的是取决于有关职能的使用模式。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签