JavaScript Learning Note (1)) —————— Call,apply method

Source: Internet
Author: User

Learning the front end also has a period of time, but the effect is very tiny. The use of time is not sufficient, although the use of work to learn. But that doesn't make me an excuse.

Today I studied (actually watched many times) call apply method.

functionABC (A, b) {returnA +b;}functioncall1 (num1,num2) {returnAbc.call ( This, num1,num2);//The call method passes a parameter in one pass}functionapply1 (num1,num2) {returnAbc.apply ( This, [num1,num2]);//here the Appl method passes the arguments that need to be an array, in the indeterminate pass                                       //The amount of time to pass the arguments is very convenient. }//Action (1): Calling functionCall1 (10,20);// -Apply1 (10,20);// -//Action (2): Extended scopeWindow.color = ' Red ';//color = ' Red ' is also possible, but with window opening is better for understanding//define an Obj objectvarobj ={color:' Blue '}varObj2 ={color:' Green '}functionSaycolor () {Console.log ( This. color);} Saycolor.call ( This);//' red '//in non-strict mode This===windowSaycolor.call (window);//' Red 'Saycolor.apply ( This);//' Red 'saycolor.apply (window);//' red '//without the second parameter, the call,apply uses no differenceSaycolor.call (obj);//' Blue 'Saycolor.apply (obj);//' Blue '//call,apply can do a good job of decouplingSaycolor.call (OBJ2);//' Green 'Saycolor.apply (OBJ2);//' Green '//(3) in the use of constructor inheritance, to inherit propertiesfunctionM (x, y) { This. x =x;  This. y =y; return  This. x + This. Y;}functionMchild (a,b,c) {M.call ( This, A, b);  This. C =C;}varM1 =NewMchild (10,20,30); Console.log (m1.x); //TenConsole.log (M1.Y);// -Console.log (m1.c);// -

  

JavaScript Learning Note (1)) —————— Call,apply method

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.