Examples of call,apply,bind function usages in JavaScript _javascript tips

Source: Internet
Author: User

This example describes the use of Call,apply,bind functions in JavaScript. Share to everyone for your reference, specific as follows:

A. Call function

A.call (b);

Simple understanding: Apply the method of object A to the B object (a If there is this, point to B)

Use of Call (): Used on functions

var dog=function () {
  this.name= "doggy person";
  This.shout=function () {
  alert (this.name);
  }
};
var cat=function () {
  this.name= "Meow Star";
  This.shout=function () {
  alert (this.name);
  }
};
var dog=new dog ();
var cat=new cat ();
Dog.shout ();
Cat.shout ();

If there is no shout method in the Cat function, to achieve the same effect

var dog=function () {
  this.name= "doggy person";
  This.shout=function () {
  alert (this.name);
  }
};
var cat=function () {
  this.name= "meow";
};
var dog=new dog ();
var cat=new cat ();
Dog.shout ();
Dog.shout.call (cat);
Cat.shout ();

Call function: Functions can be reused

Ii. Apply function

var xh=
{
  name: "Little Red",
  job: "Front-End Engineer"
};
var xm=
{
  name: "Xiaoming",
  job: "JS engineer"
};
var xw=
{
  name: "Xiao Wang",
  job: "HTML5 engineer"
};
function Myjob (gender,age,company)
{
  alert (this.name+), "+gender+", this year "+age+" years, in "+company+" as "+this.job" );
}
Myjob.call (XH, "female", 24, "Tencent");
Myjob.call (XM, "male", 22, "Sina");
Myjob.call (XW, "male", 28, "NetEase");

The call function and the Apply function function, the difference is that the second parameter form is different, call pass multiple parameters, arbitrary form (passed parameters and functions accepted parameter one by one corresponds), apply the second parameter must be an array form, such as A.call (b,2,3); ==> a.apply (b,[2,3]);

var xh=
{
  name: "Little Red",
  job: "Front-End Engineer"
};
var xm=
{
  name: "Xiaoming",
  job: "JS engineer"
};
var xw=
{
  name: "Xiao Wang",
  job: "HTML5 engineer"
};
function Myjob (gender,age,company)
{
  alert (this.name+), "+gender+", this year "+age+" years, in "+company+" as "+this.job" );
}
Myjob.apply (xh,["female", 24, "Tencent"]);
Myjob.apply (xm,["male", 22, "Sina"]);
Myjob.apply (xw,["Male", 28, "NetEase"]);

Third, bind function

Call,apply and bind can "kidnap" this, forcing it to point to other objects

The difference between use and call,apply, such as

Xw.say.call (XH);     Call
xw.say.apply (XH) directly on the function;   Call
Xw.say.bind (XH) directly on the function ();   The return is still a function, so the following need () to invoke the

It can be like call when you pass a parameter

Xw.say.bind (XH, "Central University", "First Grade") ();

Because BIND is still returning a function, it can also be invoked at the time of the call

Xw.say.bind (XH) ("Central University", "first grade");

For more on JavaScript related content to view the site topics: "JavaScript object-oriented Tutorial", "javascript json operation tips Summary", "JavaScript switching effects and techniques summary", " JavaScript Search Algorithm Skills summary, javascript error and debugging skills Summary, JavaScript data structure and algorithm skills summary, JavaScript traversal algorithm and skills summary and JavaScript mathematical Operational Usage Summary

I hope this article will help you with JavaScript programming.

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.