Analysis of _javascript techniques for JS overload implementation

Source: Internet
Author: User

The method of JS overload implementation is analyzed in this paper. Share to everyone for your reference, specific as follows:

Overload is an important feature of object-oriented language, JS does not really overload, is simulated (because JS is an object-based programming language, not purely object-oriented, it does not have real polymorphism: such as inheritance, overload, rewrite)

When do I use heavy duty?

Example:

function GetDataTop10 () {
  alert ("Return first 10 data");
}

Later demand changed, need to get 20 data, and GETDATATOP10 is called by many functions, the whole replacement can but trouble, then you can use overloaded

Which leads to what is Overload: The function name is the same, but it can be distinguished by which one you want to use, rather than the following function with the same name as the previous

Second, how to use?

A built-in object in the Arguments:js that contains the actual arguments passed by the caller (the number of parameters defined when the function declaration does not affect)

function GetData () {
  alert ("Return all Data");
}
function Getdatatop (num) {
  alert ("before returning" +num+ "data");
}
function Getdatalimit (num1,num2) {
  alert ("before returning" +num1+ "-" +num2+ "data");
}
function Getdste () {
  var num=arguments.length;
  if (!num) {
  getData ();
  }
  else if (num==1) {
  getdatatop (arguments[0]);
  }
  else if (num==2) {
  getdatalimit (arguments[0],arguments[1]);
  }
Getdste (5)

When a lot of parameters to write a large string, so you can improve

function GetData () {
  alert ("Return all Data");
}
function Getdatatop (num) {
  alert ("before returning" +num+ "data");
}
function Getdatalimit (num1,num2) {
  alert ("before returning" +num1+ "-" +num2+ "data");
}
function Getdste () {
  var num=arguments.length;
  if (!num) {
    getData ();
  }
  else if (num==1) {
    getdatatop.apply (this,arguments);
  }
  else if (num==2) {
    getdatalimit.apply (this,arguments);
  }
}
Getdste (5)

What if the number of parameters is the same and the type is different

function GetData () {
  alert ("Return all Data");
}
function Getdatatop (num) {
  alert ("before returning" +num+ "data");
}
function Getdatalimit (num1,num2) {
  alert ("before returning" +num1+ "-" +num2+ "data");
}
function Getdatabytype (type) {
  alert ("returns data of type +type+");
}
function Getdste () {
  var num=arguments.length;
  if (!num) {
  getData ();
  }
  else if (num==1) {
  if (typeof arguments[0]== "number") {
      getdatatop.apply (this,arguments);
  }
  else{
    getdatabytype.apply (this,arguments);
  }
  else if (num==2) {
  getdatalimit.apply (this,arguments);
  }
}
Getdste ("array")

Third, benefits

Conducive to cooperative development

Iv. Disadvantages

"Implementation" too reluctantly, if too many parameters, there will be a lot of if statements, it appears that the code is very messy

For more on JavaScript related content to view the site topics: "JavaScript object-oriented Tutorial", "javascript json operation tips Summary", "JavaScript switching effects and techniques summary", " JavaScript Search Algorithm Skills summary, javascript error and debugging skills Summary, JavaScript data structure and algorithm skills summary, JavaScript traversal algorithm and skills summary and JavaScript mathematical Operational Usage Summary

I hope this article will help you with your JavaScript programming.

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.