Call function and apply function

Source: Internet
Author: User

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:

  1. function A (NAMEA) {
  2. This.name=namea;
  3. This.showname=function () {
  4. return this.name;
  5. }
  6. }
  7. function B (NAMEB) {
  8. A.call (THIS,NAMEB);
  9. This.showtest=function () {
  10. return NAMEB;
  11. }
  12. Duplicate Name method
  13. This.showname=function () {
  14. return "Soul";
  15. //  }
  16. }
  17. var b=new b ("B");
  18. 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: 
  1. function A (NAMEA) {
  2. This.name=namea;
  3. This.showname=function () {
  4. return this.name;
  5. }
  6. }
  7. function B (NAMEA,NAMEB) {//parameter will be passed to a () sequentially, so the parameter of A is written in the front
  8. var arr=new Array ();
  9. Arr[0]=nameb;
  10. A.apply (This,arr); Parameter must be an array
  11. A.apply (this,arguments);
  12. This.showtest=function () {
  13. return NAMEB;
  14. }
  15. }
  16. var b=new b ("A", "B");
  17. 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

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.