简体   繁体   中英

How do I set a callback function parameter like 'data' in the “jQuery.ajax({ success : function(data)”?

I am wondering how to set the first parameter of a callback function to what I want, the same way jquery does in the success callback or the complete callback

I want to do this:

$.ajax({
  success: function(data) {
    alert(data)
  }
});

from what I understand this is as close as I can get to what I want

function test(text) {
  this.text = text
  this.success = function(this.text) { }
}

var a = new test('King Kong')
a.success = function(k){
  alert(k)
}

and I want the alert to say "King Kong"

Here's an example of accepting a callback in your constructor function and then calling it later in response to some trigger. In the below, the trigger is someone calling the trigger function, but it can be anything you want:

function Test(text, callback) {

  this.text = text;
  this.success = callback;
}

Test.prototype.trigger = function() {
    // Call the success callback, passing in the text
    this.success(this.text);
};

var a = new Test('King Kong', function(k) {
    alert(k);
});

a.trigger();

(I've made Test initially-capped there. That's the convention for constructor functions, which you are of course free to ignore.)

The key, fundamental thing to understand is that functions are just objects like anything else. You can pass references to them around, etc. To call a function, you just access whatever variable you have the function reference stored in and add parentheses (optionally with the function's arguments in the parentheses).

Consequently, all of the following call the foo function and trigger the alert:

function foo(msg) {
    alert(msg);
}

var f = foo;   // No parens, just getting the function reference, not calling it
f("Hi there"); // Now we're calling it
var a = {};
a.nifty = f;
a.nifty("Hi again");

function bar(func) {
    func("Hello for the third time");
}
bar(foo);     // Passing a reference to `foo` into the `bar` function, which will call it

Advanced : Now, one thing that jQuery does is that it calls the callback with the this value set to something specific (usually the DOM element related to the call). That will happen any time you call a function via an object property:

var a = {name: "Fred"};
a.func = function() {
    alert(this.name);
};
a.func(); // alerts "Fred"

...but that's not the only way you can do it; there's also the call and apply functions on the function object itself:

var a = {name: "Fred"};
function func() {
    alert(this.name);
}
func.call(a); // alerts "Fred"

There, the function isn't assigned to any of a 's properties, but we've called the function using call , which accepts the value for this as its first argument. Call also passes on any further arguments to the function you're calling:

function func(msg1, msg2) {
   alert(this.name + " says " + msg1 + " and " + msg2);
}
var a = {name: "Fred"};
func.call(a, "one", "two"); // alerts "Fred says one and two"

apply does exactly the same thing, but it accepts the arguments to pass on to the underlying function as an array instead of as discrete arguments:

function func(msg1, msg2) {
   alert(this.name + " says " + msg1 + " and " + msg2);
}
var a = {name: "Fred"};
func.apply(a, ["one", "two"]); // alerts "Fred says one and two"
//            ^------------^----- note these args are now an array

More reading: Mythical Methods

I figured out what I was looking for, it was this

function Test(data) 
  {
    data.success(data.text)
  }

  var a = new Test({
    text : 'DATA',
    success : function(k) {
      alert(k)
    }
  })

or

 function Test() 
  {
    this.setText = function(k) {
      this.text = k
    }
    this.setCall = function(k) {
      this.call = k
      this.call(this.text)
    }
  }

  var a = new Test
  a.setText('DATA')

  a.setCall(function(k) {
    alert(k)
  })

both alert "DATA"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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