Good programmers share function test of the Js web

  Js web programmers to share good function of the test , in JS, in general, in fact, use the function to package certain operations, or to programs written in modular operation.

  A . Declaratively function

  1. Ordinary function declaration

  function box(num1, num2) {

  return num1+ num2;

  }

  2. Use the variable initialization function

  var box= function(num1, num2) {

  return num1 + num2;

  };

  3. Function constructor

  var box= new Function('num1', 'num2' ,'return num1 + num2');

  Two as a function of the value of

  ECMAScript function name in itself is variable, the function can also be used as a value. In other words, not only can pass parameters like the same pass a function to another function, and a function can be returned as a result of another function.

  function box(sumFunction, num) {

  return sumFunction(num);

  }

  I function (num) {

  return num + 10;

  }

  var result = box(sum, 10);

  Three . Function internal property

  Inside the function, there are two special objects: arguments The and this. arguments is an array object class, contains all the parameters passed to the function. The main purpose is to function arguments. But this object also named callee's property, which is a pointer to a function that has arguments object.

  function box(num) {

  if (num <= 1) {

  return 1;

  } else {

  * whether the return box (num-1);

  }

  }

  For the factorial function is generally to use a recursive algorithm, so the internal function will call itself ; if the function name does not change is not a problem, but once changed the function name, calling itself the internal need to modify one by one. To solve this problem, we can use arguments.callee instead.

  function box(num) {

  if (num <= 1) {

  return 1;

  } else {

  return num * arguments.callee(num-1);

  }

  }

  Another special object is an internal function this, the behavior of the Java C # in this and similar. In other words, this refers to a function, according to the object to perform an operation, or function call statement that the scope is located. PS: When you call a function in the global scope, this is the object reference window.

  window.color = 'Red';

  alert(this.color);

  each box = {

  color: 'blue',

  sayColor : function () {

  alert(this.color);

  }

  };

  box.sayColor();

  alert(this.color);

  Four Functions Properties and Methods

  ECMAScript is a function of the object, and therefore functions have attributes and methods. Each function contains two properties: length and prototype. Wherein, length property represents the number of desired functions named parameters received.

  function box(name, age) {

  alert(name + age);

  }

  alert(box.length);

  function box(num1, num2) {

  return num1 + num2;

  }

  function sayBox(num1, num2) {

  return box.apply(this, [num1, num2]); }

  function sayBox2(num1, num2) {

  return box.apply(this, arguments);

  }

  alert(sayBox(10,10));

  alert(sayBox2(10,10));

  call () method () in the same manner apply, they differ only in the way the received parameters. For call () method, the first parameter is the scope, no change, change is only the remaining parameters are passed directly to the function.

  function box(num1, num2) {

  return num1 + num2;

  }

  function callBox(num1, num2) {

  return box.call(this, num1, num2);

  }

  alert(callBox(10,10));

  var color = 'Red';

  each box = {

  color: 'blue'

  };

  function sayColor() {

  alert(this.color);

  }

  sayColor();

  sayColor.call(this);

  sayColor.call(window);

  sayColor.call(box);

  When we use the time to call (box) method, the runtime environment sayColor () method has become a box object.

  Use call () or apply () to expand the scope of the biggest benefits is that the object does not require any coupling relationship and coupling methods occur, the meaning is interrelated, expand and maintain a chain reaction will occur.

Guess you like

Origin www.cnblogs.com/gcghcxy/p/11389022.html