Call Apply JS

Source: Internet
Author: User

It seems that an object does not have a method to process the data, a method has no object data, so that the two can SquarePants underwater, just fine.
----

A function that uses the properties of another object through its own method to achieve some kind of change or the purpose of outputting data.

2015-5-4--is a function. Call (Object A) the properties of object A will be moving into this function, and will naturally output a value or change something.

==============

Call () and apply () are the own methods that each function contains. It has been mentioned before that a function is a defined object, so when calling a function, this is a call to the current and next variable. If you want to change the domain space where the function executes, you can use call () and apply ().

color = ' red ';

var o = {color: ' Blue '};

function Saycolor () {

Console.log (This.color);

}

Saycolor (); Red

Saycolor.call (this); Red

Saycolor.call (o); Blue = = = Popular understanding is to connect objects, let their own methods. The money to help people to let others play.

/////////////////////////////////////////////////////////////////////

http://www.zhihu.com/question/20289071

Http://www.cnitblog.com/yemoo/archive/2007/11/30/37070.aspx

=================

LZ to understand the existence of call and apply the reason, to remember a bit:

In JavaScript oop, we often define this:

function Cat () {

}

cat.prototype={

Food: "Fish",

Say:function () {

Alert ("I Love" +this.food);

}

}

var blackcat = new Cat;

Blackcat.say (); But if we have an object Whitedog = {food: "Bone"}, we do not want to redefine it say method, then we can use call or Apply with Blackcat say method: BlackCat.say.call (White
BlackCat.say.call (Whitedog);

So, you can see that call and apply are meant to change this dynamically, and when an object does not have a method, but the others, we can use call or apply to manipulate other objects.

The DOM node chosen by document.getElementsByTagName is a type of array that is similar to array. It cannot apply methods such as Push,pop under the array. We can do this by:
var domnodes = Array.prototype.slice.call (document.getElementsByTagName ("*"));
This allows the domnodes to apply all the methods under the array.

Call Apply JS

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.