One, Function.call () function
1, A.call (b)??; include 3rd [pass. Call (b) before object to B object]//much like inheritance
Pass the A object (all) to the B function object????;
2, A.amethod.call (b)?; Does not overwrite the same name method in B
Pass the Amethod method of? A to the B function object;
3. The test code is as follows:
- function A (NAMEA) {
- This.name=namea;
- This.showname=function () {
- return this.name;
- }
- }
- function B (NAMEB) {
- A.call (THIS,NAMEB);
- This.showtest=function () {
- return NAMEB;
- }
- Duplicate Name method
- This.showname=function () {
- return "Soul";
- // }
- }
- var b=new b ("B");
- Console.log (b.name+ "" +b.showname () + "" +b.showtest ()); b b b (minus the comment The result is: b Soul b)
Second, function.apply function 1, function.apply (obj,arguments) the args parameter will be passed to the function functions (starting with arguments[0] in turn 2. The code example is as follows:
- function A (NAMEA) {
- This.name=namea;
- This.showname=function () {
- return this.name;
- }
- }
- function B (NAMEA,NAMEB) {//parameter will be passed to a () sequentially, so the parameter of A is written in the front
- var arr=new Array ();
- Arr[0]=nameb;
- A.apply (This,arr); Parameter must be an array
- A.apply (this,arguments);
- This.showtest=function () {
- return NAMEB;
- }
- }
- var b=new b ("A", "B");
- Console.log (b.name+ "" +b.showname () + "" +b.showtest ()); A A B
Third, apply other uses 1, Math.max.apply (Null,arr); Returns the maximum value of an array in arr 2, Array.prototype.push.apply (ARR1,ARR2); Adding arr2 to arr1 arr1 itself changes
Call function and apply function