Good programmers web front-end function Js web to share learning course of the test

  Good programmers web front-end function Js web to share learning course of the test, in JS, in general, in fact, use the function to package certain operations, or to programs written in modular operation.
  A declarative function
  1. The function declaration ordinary
  function Box (num1, num2) {
  return num1 num2 +;
  }
  2. Use a variable initialization function
  var = Box function (num1, num2) {
  return num1 num2 +;
  };
  . 3. use function constructor
  var Box = new new function ( 'num1', 'num2', 'return num1 + num2');
  . two as a function of the value of
  the function name in ECMAScript is itself variable, the function may be used as the 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.
  Box function (the SumFunction, NUM) {
  return the SumFunction (NUM);
  }
  function SUM (NUM) {
  return NUM + 10;
  }
  var Box Result = (SUM, 10);
  . internal property three functions
  Inside the function, there are two special objects: arguments 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.
  Box function (NUM) {
  IF (NUM <=. 1) {
  return. 1;
  } the else {
  return NUM Box (. 1-NUM);
  }
  }
  for the factorial function is generally to use a recursive algorithm, the internal function will call itself; if It does not change the function name 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.
  Box function (NUM) {
  IF (NUM <=. 1) {
  return. 1;
  } the else {
  return NUM
The arguments.callee (. 1-NUM);
  }
  }
  internal function further special object is this, which behaves in Java and C # this roughly 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);
  Box = {var
  Color: 'blue',
  sayColor: function () {
  Alert (this.color);
  }
  };
  box.sayColor ();
  Alert (this.color);
  four function attributes and methods.
  the ECMAScript in functions are objects, functions and therefore have attributes and methods. Each function contains two properties: length and prototype. Wherein, length property represents the number of desired functions named parameters received.
  Box function (name, Age) {
  Alert (name + Age);
  }
  Alert (box.length);
  function Box (num1, num2) {
  return num1 num2 +;
  }
  function sayBox (num1, num2) {
  return box.apply ( the this, [num1, num2]);}
  function sayBox2 (num1, num2) {
  return box.apply (the 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.
  Box function (num1, num2) {
  return num1 num2 +;
  }
  function callbox (num1, num2) {
  return box.call (the this, num1, num2);
  }
  Alert (callbox (10,10));
  var Color = 'Red the ';
  var = {Box
  Color:' blue '
  };
  function sayColor () {
  Alert (this.color);
  }
  sayColor ();
  sayColor.call (the this);
  sayColor.call (window);
  sayColor.call (box);
  when we use the 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 blog.51cto.com/14479068/2431382