The difference between function.apply () and Function.call ()

Source: Internet
Author: User
Tags traits

The following content is translated from StackOverflow

Link:

Http://stackoverflow.com/questions/7238962/function-apply-not-using-thisarg-parameter

In AS3, method is different from function, which is part of a class and is bound to an instance "that is, once the class is instantiated, the method defined in the class binds to the instance", looking at the second part of the link about Method, Reference part:

Methods is the functions (function) of a class definition, and once the class is instantiated, method binds to the instance. Functions can be declared outside the class, and methods cannot be used separately from the class.

So, when we create a myobj class, all the methods in the class bind to this instance, which is why when you want to use call or apply (the first parameter), and do not find this (the pointer) is redirected to the new object. Look at the details of bound methods's explanation. (Then there is time to translate bound methods)

Look at this link explaining traits object (with time translation), ActionScript is used to solve method, and is used as a performance reason behind what might be blamed, the traits object and the class method are just the syntactic sugars that follow the ECMAScript pattern:

var TestClass = function (data) {    varthis;     this. data = data;     this. Boundwork = function () {        return  Self.constructor.prototype.unboundWork.apply (self, arguments);     = function () {    returnthis. data;};

(The above-mentioned wording, I think is not added to the syntax of sugar, it seems to understand)

varA =NewTestClass ("a");varb =NewTestClass ("B"); alert (A.boundwork ()); //aAlert (B.boundwork ());//balert (A.unboundwork ());//aAlert (B.unboundwork ());//balert (A.boundwork.call (b));//aAlert (A.boundwork.call (undefined));//aalert (A.unboundwork.call (b));//b

Or more interesting notation:

var method =//  undefined. ack!

To compare:

method =//  A. TADA magic!

Note that the execution of Boundwork is always subject to the instance in which it belongs, no matter what object you change this to, and how it is called with the call and apply methods. In ActionScript, this approach explains why a method in a class is bound to an instance of it. So no matter where these two methods are used, they always point to their corresponding instances (which is a bit like the event model in ActionScript). Once you understand it, this roundabout solution can be understood. (Here the author uses a very interesting word work-around)

(Explanation: Work-around

Chinese explanation

It's not a fundamental solution, but a way to avoid problems.

Avoid problems or difficulties and walk to the end of the road.

Rights; Should be the urgent policy.

Originally a computer language, relative to "fix". When a program has a problem, find out where the problem lies and then directly resolve it called "fix"; When the problem is not resolved, it is a way to ignore this problem and make it do not affect the purpose that you want to use this program, this method is called workaround.

English explanation

Workaround means a manner of bypassing a problem caused by a bug without correcting the bug itself.

Workaround is similar to "stopgap solution". If There is a problem, a "workaround" doesn ' t eliminate the problem, but it does bypass the problem.

)

(Own subjective understanding:)

In some places, if you want to declare a method in a class, and you want to do your own thing in this way (changing the object that this method refers to is overriding this), (an empty translation prototype the function usage, with a little abstraction) you can write:

package{ImportFlash.display.Sprite;  Public classFunctionapplytestextendsSprite { Public functionfunctionapplytest () {varObja:myobj =NewMYOBJ ("A"); varObjb:myobj =NewMYOBJ ("B");            Obja.sayname ();            Objb.sayname (); ObjA.sayName.apply (OBJB, []); //aObjA.sayName.call (OBJB);//aObjA.pSayName.call (OBJB)//B <---}}}internalDynamic classmyobj{Private var_name:string;  Public functionMYOBJ (name:string) {_name=name; }        Public functionSayname ():void{trace (_name); } prototype.psayname=function():void{Trace ( This. _name); };}

In the above code, serious attention to the difference between Sayname and Psayname, Sayname has been bound with the instance, but Psayname is different, it can be used by myobj instances, but not with a specific instance binding.

The difference between function.apply () and Function.call ()

Related Article

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.