Recommendation 16: The comparison function call mode

  In JavaScript, the function is the object, the object is the "name / value" of the collection, and has a hidden connection to prototype objects. Objects object literal produced connected to object.prototype, function object connected to Function.prototype, the prototype the object itself is connected to object.prototype each function when you create has two additional hidden attribute: context and implement behavior of the function code for the function.

  Each function also will be created when the object with a prototype property, whose value is a constructor property and has an object constructor attribute value of the function. This is connected to the hidden Function.prototype completely different. This is connected to the hidden Function .prototype completely different.

  , Function like other values ​​can be used as an object variable at any position as a function of array elements can be used as the return value of the function, the value of the object can be a member of, or as an expression parameter calculation, and function may also have their own method.

  Call a function to suspend execution of the current function, and passes control parameters to the new function in addition to the formal parameters defined when you declare, every function accepts two additional parameters:.. This parameter and the arguments in this very important object-oriented programming its value depends on calling patterns in JavaScript, a total of. four kinds of call mode: the method call mode, the function call mode, the constructor call mode and call mode apply these patterns differ on how to initialize the key parameters of this..

  A pair of parentheses call operator is following the degree of any expression to produce a function of the value, in parentheses can contain zero or more expressions separated by commas. Each expression generates a parameter value, each of parameter when the parameter value being assigned function declaration defined when the number of actual parameters (arguments) and the number of formal parameters (parameters) mismatch does not result in run-time error. If the argument is too large, exceeding the value of the parameter is ignored. If the argument is too small ,, then the missing values ​​will be replaced. will not check the type of the parameter value is undefined, the value of any type can be passed to the parameter.

  (1) method call mode

  When a function is stored as a property of the object, a method will be referred to when a method is invoked, the this is bound to the object. If the expression contains a call to access a property expression (i.e., expression of a point type or [subscript] subscript expression, then it is treated as a method to call).

var obj = {
  value:0,
  increment:function(inc){
    this.value += typeof inc === 'number'?inc:1;
  }
}
obj.increment()
console.log(obj.value);   //1
obj.increment(2)
console.log(obj.value);   //3

  Since the increment can use this method to access the object, so that he or modify the value of the object-to-object binding .this occurs when the call from the object. This late binding so that this function can be taken for college, through this may Gets increment method belongs to the object context method is called public methods.

  (2) function call mode

  When a property is not a function object, it will be treated as a function call 

var  sum =add(3,4)  //7

  When the function call in this mode, this is bound to the global object. This is a mistake on the design of the language. If the language is designed properly, when the internal function is called, this should still be bound to this variable outside the function, the methods can not be used when the consequences of the wrong design of the internal functions to help it work, because this function is bound inside the wrong value, it can not share access to the object's method. when Luckily, there is a very easy to solve : If the method to define a variable and assign it to this, so internal functions can access this via this variable.

var obj = {
  value:1,
  doub:function(){
    var that = this;
    var helper = function(){
      that.value = that.value*2
    }
    helper()
  }
}
obj.doub();
console.log(obj.value);   //2

  (3) configured to call mode

  JavaScript is a prototype-based inheritance language, which is classless, objects can inherit properties from other objects. Most of today's dollar is class-based languages, although prototype inherited with a strong performance, but it deviated from the mainstream usage, is not widely understood .Javascript writing style to be able to build on class-based language, provides an object based on a structured like a language syntax.

  If in front of a function plus the new operator to make the call, it will create a hidden link to a new instance of an object prototype prototype object of this function, while this will be binding on the new instance of the object. Note that new prefixes We will change the behavior of the return statement

var F = function(string){
  this.status = string;
}
F.prototype.get = function(){
  return this.status
}
var f = new F("new object")
console.log(f.get());    //new object

  The above code creates a constructor function F is called, the function constructed with the object status attribute. Then, the F get all instances to provide a public method named last create an instance of the object, and to call the get method read status attribute value.

  Combined with new function called prefix calls the constructor function. By convention, the constructor function should be saved to the variable name with uppercase in. If you call the constructor is not in plus new front , very bad things can happen, both warning warning did not run when there is no compile-time, so capitalization convention is very important.

  (4) apply to call mode

  JavaScript is a functional object-oriented programming languages, a function can have a basic method is a method .apply function, the function can be called using this method, and this value modification function in vivo .apply method includes two parameters: a first parameter binding to this value; a second parameter is a function of parameters, for example, an array comprising:

var array = [5,4]
var add = function(){
  var i,sum=0;
  for(i=0;i<arguments.length;i++){
    sum += arguments[i]
  }
  return sum
}
var sum = add.apply({},array)
console.log(sum);    //9

  Method calls the apply add () function, the array elements in the array values ​​are added.

var F = function(){
  this.status = string
}
F.prototype.get = function(){
  return this.status
}
var obj = {
  status:'obj'
}
var status = F.prototype.get.apply(obj)
console.log(status);  //obj

  The above code is constructed a constructor F., Defines a prototype for the function get method, the method can read the current value of the object status attribute. Then define a obj object that contains a status attribute using methods apply obj calling the constructor method on the object F to get return status attribute value of the object obj.

Guess you like

Origin www.cnblogs.com/chorkiu/p/12118372.html