JavaScript Learning notes 2-functions

Source: Internet
Author: User

2. function

2.1 Function Direct Quantity

The direct amount of a function is used as an expression, not as a statement, and it does not need to specify a function name. The comparison with the function:

JS Code

function f(){}
var f = function(){}

2.2 Parameters of the function

JS in the function of the number and type of parameters can be very arbitrary, which needs to be used for the actual situation of the corresponding judgement processing.

Optional parameters

If the parameter of a function is optional, then if it is not transmitted, then it is the value of underfined, in the function Assign a default value to it. This kind of writing is very common.

Use an object as a parameter to a function

When the parameters of the function are longer used, and often with the | | Used together.

Parameter type

If you have strict requirements for the parameter types of a function, be sure to judge the type within the function, and if the type is incorrect, throw an exception to report the fact. such as the Arraycopy () method, the expected argument is an array, and if the incoming is not an array, it needs to be thrown.

Again, for example, we calculate the sum of all elements of an array, and if so, in the previous case, in the array, it is predetermined that all elements are numeric and then summed according to the elements of the value. However, many times the developer is not a person, perhaps the array you are dealing with is provided to you by someone else, or if you are taking data from a database to construct a number of groups, you cannot judge the element as a numeric value, and you must judge whether the incoming is an array and whether the element is numeric. If the element is ignored when underfined or null, or if the element contains a hint when it is not a value, or when the value passed in is not an array. The specific code is as follows:

JS Code

function ArrayUserCommMgr{}
ArrayUserCommMgr.prototype={
 isArray:function(a){
  var result = false;
  if((a instanceof Array)||(a && typeof a == "object" && "length" in a)){
  result = true;
 }
 return result;
 },
 getSum:function(a){
 if (arrayUserCommMgr.isArray()){
  var total = 0;
  for(var i = 0; i < a.length; i++) {
  var element = a[i];
  if (!element) continue; // ignore null and undefined elements
  if (typeof element == "number"){
   total += element;
  }else{
   throw new Error("sum(): all array elements must be numbers");
  }
  }
 }else{
  throw new Error("sum(): argument must be an array");
 }
 }
}

The above code notes:

1, with ((a instanceof Array) | |

(a && typeof a = = "Object" && "Length" in a))

To determine whether to pass in an array (instanceof array), or an array-like object ((a && typeof a = = "Object" && "Length" in a)

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.