The function also has two very useful properties: length and prototypefunctionfn1 () {}functionfn2 (num1,num2) {}functionFn3 (NUM1) {}//The length of the function indicates the value of the parameter expected by the functionAlert (fn1.length)//0Alert (fn2.length)//2Alert (fn3.length)//1call and apply methods can be called by the function name of the function has two parameters for apply, the first is the co
In JS, call and apply their role is to bind the function to another object to run, the two only in the definition of the parameters of the way there are differences, let me give you a brief introduction of the use of calling and apply.In the Web front-end development process, we often need to change this point, usually we think of the call method, but for the understanding of call many people are not very clear, then the following small series to give
The apply and call functions are the same, but they have different parameters.
1. call
Call Method
Call a method of one object to replace the current object with another object.
Call ([thisObj [, arg1 [, arg2 [, [,. argN])
Parameters
ThisObj
Optional. Will be used as the object of the current object.
Arg1, arg2, and argN
Optional. The method parameter sequence will be passed.
Description
The call method can be used to call a method instead of another
JS in the execution of the function of two ways: one by calling the operator ' () ', and the second by invoking call or apply to execute dynamically.The This object is specified in a dynamic method callIn development we often need to invoke the method of object A in object B, this time using apply () and call (), their first parameter is used to specify the This object, if NULL, the default host object is p
1. method definition
Call method: Syntax: call ([thisObj, arg1, arg2, arg ...... N]), the parameter arg can only be in the form of a list, such as: a, B, c Definition: Call one method of an object and replace the current object with another object. Note: The call method can be used to call a method instead of another object. The call method can change the object context of a function from the initial context to the new object specified by thisObj. If the thisObj parameter is not provided, the G
IOS --- How to apply for an Apple developer account process detailed graphic introduction (including Deng Bai's coding application method detailed introduction)
Because the company needs to migrate the membership of the previously applied personal Apple developer account to the company name (that is, the personal work is displayed when our app was uploaded to the app stroe, after the TV is migrated, the company name will be displayed in the app, and t
SummaryFunction. Prototype. Apply (thisarg, argarray );
Function. Prototype. Call (thisarg [, arg1 [, arg2…]);
After Method A of obj1 is bound to obj2 for running, the runtime environment of function a is transferred to obj2,That is, the this Pointer Points to obj2.
Bytes ----------------------------------------------------------------------------------------------------------------------Apply, call, a
. showAdd = function (a, B)
{
Alert (a + B );
}
}
Function Class2 (){Class10.call (this );Class11.call (this );}
It's easy to implement multi-inheritance with two calls.Of course, there are other methods of js inheritance, such as using prototype chain. This is not part of the scope of this article, but only describes the call usage here.When we talk about call and apply, these two methods basically mean one thing.The difference is that the second pa
Write todayCodeThePython applyThe method is very easy to use, so I wroteArticleThis article will be introduced to you.Python apply function:
The apply (func [, argS [, kwargs]) function is used to call a function indirectly when a function parameter already exists in a tuple or dictionary. ARGs is a tuple that contains parameters that will be provided to the function by location. If ARGs is omitted, no pa
Why What's call,apply,bind doing? Why do you want to learn this?This is typically used to specify the environment for this, which is usually the problem before it is learned.var a = { User: "Chasing the Dream", fn:function () { console.log (this.user); }} var B = a.fn;b (); UndefinedWe want to print the user in object A but print out what's going on with the undefined? If we directly execute A.fn () is possible.var a = { User: "Chas
replace the values, calls, and deletion codes of the first three rows:Copy codeThe Code is as follows:Function ClassA (sColor ){This. color = sColor;This. sayColor = function (){Alert (this. color );};}
Function ClassB (sColor, sName ){// This. newMethod = ClassA;// This. newMethod (color );// Delete this. newMethod;ClassA. call (this, sColor );
This. name = sName;This. sayName = function (){Alert (this. name );};}Here, we need to make the keyword this in ClassA equal to the newly created Class
JavaScript functions 定义时上下文 运行时上下文 have concepts such as "" and "" and " 上下文是可以改变的 "apply and call functions
Call () and apply () all exist to change a function 运行时的上下文 (context), in other words, to change the direction of this within the function body.
The call () method accepts one 参数列表 , and the Apply () method accepts a 数组 (or class array object)
Really is very simple to understand, I know not much, in the Internet to find a lot of information, or only to understand a little fur, the following to organize out, to facilitate further in-depth to learn, but also to the current knowledge point of consolidation.Sort out some of the classic answers on the Web:1. One sentence distinguishes between call and applyObj1.call (OBJ2,ARG1,ARG2,ARG3) = = Obj1.apply (obj2,arguments) = = Obj2.foo (ARG1,ARG2,AR
obtaining an instance of test?2. Use call, apply, and bind to change the context in which the function executes (this)Using call, apply, and bind can be the context of a function change, so let's take a look at the difference between the reporters.Call Method:Syntax: Call ([thisobj[,arg1[, arg2[, [,. ArgN]]])Definition: Invokes one method of an object, replacing the current object with another object.Descr
need to replace the values, calls, and deletion codes of the first three rows:
Copy codeThe Code is as follows: function ClassA (sColor ){
This. color = sColor;
This. sayColor = function (){
Alert (this. color );
};
}
Function ClassB (sColor, sName ){// This. newMethod = ClassA;// This. newMethod (color );// Delete this. newMethod;ClassA. call (this, sColor );
This. name = sName;This. sayName = function (){Alert (this. name );};}
Here, we need to make the keyword this in ClassA equal to the ne
in learning JavaScript, the use of call () and apply () is always difficult to understand, I believe many small partners and I have the same idea. Recently in the Rhino book, the two functions have a hard understanding, finally some clues, understand the basic usage of two functions, and now share with the small partners, said the wrong still hope to correct. First, callSyntax: Call ([thisobj[,arg1[, arg2[, [,. ArgN] ]])Definition: Invokes one method
The call, apply, bind method one, call (), and apply () methods in JS1. Method definitionsCall Method:Syntax: Call ([thisobj[,arg1[, arg2[, [,. ArgN]]])Definition: Invokes one method of an object, replacing the current object with another object.DescriptionThe call method can be used to invoke a method in place of another object. The call method can change the object context of a function from the initial c
current model value in scope differs from the model value that was last computed. If it is different, then the corresponding callback function will be executed. The result of invoking the function is that the contents of the expression in the view are updated.Angularjs does not call $digest () directly, but instead calls $scope. $apply (), which calls $rootscope. $digest (). Therefore, the round $digest cycle starts at $rootscope and then accesses al
The function of call and apply in JS is to change function calling object, implement inheritanceFor example:Changing the Calling Objectfunction A (x, y) { alert (x+y);} function b (x, y) { alert (x-y);} A.call (b,1,5)//The A method is given to B with a result of 5Change this pointFunction B () { alert (this)}b ();//windowb.call ();//windowb.call ("a", 2,3);//aMethod B is defined as a Global object window when B.call ("a"), method B has = is c
In JavaScript, both apply and call are created in order to change the context in which the function is run, and, plainly, to change the direction of this in the function.JavaScript has a concept of "define context" and "run Context" and "context can be changed."Call and apply both are basically exactly the same, except that the parameters are accepted in a different way.Func.call (This, arg1, arg2); Func.ap
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.