如何在JavaScript中创建自定义回调?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (8)
  • 关注 (0)
  • 查看 (110)

我所需要做的就是在当前函数执行结束时执行回调函数。

function LoadData() 
{
    alert('The data has been loaded');
    //Call my callback with parameters. For example,
    //callback(loadedData , currentObject);
}

函数如下:

object.LoadData(success);

function success(loadedData , currentObject) 
{
  //Todo: some action here 
}

我应该如何实现?

提问于
用户回答回答于

只需将回调声明为参数,您就可以直接使用参数名调用它。

基本

function doSomething(callback) {
    // ...

    // Call the callback
    callback('stuff', 'goes', 'here');
}

function foo(a, b, c) {
    // I'm the callback
    alert(a + " " + b + " " + c);
}

doSomething(foo);

doSomething会调用foo

请注意,传递函数非常重要,具体请参照(foo),而不是调用函数并传递其结果foo()

有时,可以调用回调,以便它看到this。你可以很容易地使用JavaScript的call()方法完成这一任务。

function Thing(name) {
    this.name = name;
}
Thing.prototype.doSomething = function(callback) {
    // Call our callback, but using our own instance as the context
    callback.call(this);
}

function foo() {
    alert(this.name);
}

var t = new Thing('Joe');
t.doSomething(foo);  // Alerts "Joe" via `foo`

还可以传递参数:

function Thing(name) {
    this.name = name;
}
Thing.prototype.doSomething = function(callback, salutation) {
    // Call our callback, but using our own instance as the context
    callback.call(this, salutation);
}

function foo(salutation) {
    alert(salutation + " " + this.name);
}

var t = new Thing('Joe');
t.doSomething(foo, 'Hi');  // Alerts "Hi Joe" via `foo`

有时,传递要将回调作为数组而不是单独传递的参数是有必要的。你可以通过apply做到:

function Thing(name) {
    this.name = name;
}
Thing.prototype.doSomething = function(callback) {
    // Call our callback, but using our own instance as the context
    callback.apply(this, ['Hi', 3, 2, 1]);
}

function foo(salutation, three, two, one) {
    alert(salutation + " " + this.name + " - " + three + " " + two + " " + one);
}

var t = new Thing('Joe');
t.doSomething(foo);  // Alerts "Hi Joe - 3 2 1" via `foo`
用户回答回答于
function LoadData (callback)
{
    // ... Process whatever data
    callback (loadedData, currentObject);
}

函数来自JavaScript的第一个类。

用户回答回答于

在调用回调函数时,我们可以使用以下代码:

consumingFunction(callbackFunctionName)

举例:

// Callback function only know the action,
// but don't know what's the data.
function callbackFunction(unknown) {
  console.log(unknown);
}

// This is a consuming function.
function getInfo(thenCallback) {
  // When we define the function we only know the data but not
  // the action. The action will be deferred until excecuting.
  var info = 'I know now';
  if (typeof thenCallback === 'function') {
    thenCallback(info);    
  }
}

// Start.
getInfo(callbackFunction); // I know now

Codepend有全例。

用户回答回答于
function LoadData(callback) 
{
    alert('the data have been loaded');
    callback(loadedData, currentObject);
}
用户回答回答于
   function callback(e){
      return e;
   }
    var MyClass = {
       method: function(args, callback){
          console.log(args);
          if(typeof callback == "function")
          callback();
       }    
    }
MyClass.method("hello",function(){
    console.log("world !");
});

返回:

hello world !
用户回答回答于
function loadData(callback) {

    //execute other requirement

    if(callback && typeof callback == "function"){
        callback();
   }
}

loadData(function(){

   //execute callback

});
用户回答回答于
<script>
    dosomething("blaha", function(){
        alert("Yay just like jQuery callbacks!");
    });


    function dosomething(damsg, callback){
        alert(damsg);
        if(typeof callback == "function") 
        callback();
    }
</script>
用户回答回答于

在尝试执行回调之前,确保回调是一个实际的函数是很好的做法:

if (callback && typeof(callback) === "function") {

  callback();
}

扫码关注云+社区

领取腾讯云代金券