"JavaScript" JavaScript callback function

Source: Internet
Author: User
Tags getmessage

What is a JavaScript callback function?

functions, like other data, can be assigned, deleted, copied, and so on, so you can also pass functions as arguments into another function. This function is called a callback function. Example:Casefunction A with no parameters (b, c) {return B () + C ()} function B () {return;} function C () {return 7;} Console.log (A (B, C));    case//with parameters (after reorganizing the parameters, passing in D, as the parameters of the callback function, gives us the flexibility, the parameters of the callback function, which is entirely up to us) function A (m, n, fun) {var d = m+n; return Fun (d);} function Fun (c) {return c} console.log (A (5, 4, fun)); Result (firebug test result)about:(http://www.cnblogs.com/fighting_cp/archive/2010/09/20/1831844.html)Javascript Call and Apply methodsThe call and apply methods are used to toggle the execution context of the method, which is a very cool feature. (The difference between the two is that the Parameter form is not the same, the function is the same) distinguish apply,call in a word, foo.call (this, arg1,arg2,arg3) = = Foo.apply (this, arguments) = = This.foo (arg1, ARG2, ARG3) call, Apply is a method of Function.prototype, which is implemented internally by the JavaScript engine because it belongs to the Function.prototype, so each function object instance, That is, each method has a call, apply property. As a property of a method, the use of them is of course directed towards the method. These two methods are easy to confuse because they work just the same way. call, apply method The difference is that, from the second parameter, called method parameters will be passed to the borrowed method as parameters, and apply directly put these parameters in an array and then passed, and finally the parameter list of the Borrowing method is the same.  Example: Assigning a message property to a using the Setmessage method of B Funa () {    this.message;    This.getmessage = function () {        return this.message;   } } function Funb () {    thi s.message;    this.setmessage = function (msg) {        this.message = msg;   }}&N Bsp;var B = new Funb (), var a = new Funa (), B.setmessage.call (A, "This is a ' s message!");  console.log (A.getmessage ());   //not work case://If message is a private property then Workfunction Funa () {    var message;    this.getmessage = function () {        return message;   } }& Nbsp;function Funb () {    var message;    this.setmessage = function (msg) {        Message = msg;   }} var b = new Funb (), var a = new Funa (); B.setmessage.call (A, "This is a ' s message!");  console.log (A.getmessage ());  //result (firebug test result)   Example: Add two methods to an array with a callback functionFunction.prototype.method = function (name, FN) {    This.prototype[name] = fn;    return this;}  if (! Array.prototype.forEach) {  //Here FN as a callback function, we define three parameter array elements for it, array element ordinal, array itself   Array.method (' ForEach ', Function (FN, thisobj) {    var scope = Thisobj | | window;    for (var i = 0, len = this.length; I &lt ; Len ++i) {      Fn.call (scope, this[i], I, this);   } });}  if (! Array.prototype.filter) {  Array.method (' Filter ', function (FN, thisobj) {    var scope = thisobj | | window ;    var a = [];    for (var i = 0, len = this.length; i < Len; ++i) {      if (! Fn.call (scope, this[i], I, this)) {        continue;     }      A.push (This[i]);   }    return a; });}   Example test: Function.prototype.method = Function (name, FN) {    This.prototype[name] = fn;   return this;}  if (! Array.prototype.forEach) {   array.method (' ForEach ', function (FN, thisobj) {    var scope = Thisobj | | window;    for (var i = 0, len = this.length; i < Len; ++i) {      Fn.call (scope, this[i], I , this);   } });}  //because the JavaScript parameter is variable, the number of arguments is selected when defining the callback method, but note the order var a = function (item, I, array) {    Console.log ( "array[" + i + "]," + Item + "   array:" + array);}  var array = [1, ' 163 ', 4, ' JavaScript ']; array.foreach (a);  //result: (Firebug test results)  
Other references:
http://recurial.com/programming/understanding-callback-functions-in-javascript/
http://www.impressivewebs.com/callback-functions-javascript/

http://zhenghaoju700.blog.163.com/blog/static/135859518201281072518533/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.