Function object
Functions (methods) in an object
When a function is saved as a property of an object, we call it a method, and when a method is called, this is bound to the object.
Method can use this to access the object to which you belong;
Return
A function returns a value even though it is returned. If no return value is specified, undefined is returned.
If a function call is preceded by a new prefix and the return value is not an object, this is returned (the new object);
Under jquery's On method, the return value of the binding function is obtained;
var returnvalue;//variable to store the function's return value; function A () { return 123456; } $ (". H1"). On ("Click", function A () { = A (); // 123456 });
Method invocation Pattern
When a function is saved as a property of an object, we call it a method. When a method is called, this is bound to the object. If the invocation expression contains an action that extracts a property (both a. Point expression or [subscript] subscript expression), then it is called as a method;
Function call pattern
When a function is not a property of an object, it is called as a function:
var sum = Add (3, 5); The value of sum is 8;
When a function is called in this mode, this is bound to the global object. This is a mistake in language design, for an intrinsic function of a method in an object, if we want this to point to the object, but we can do it in a simple way: define a variable in the method and assign it a value of this, so that the intrinsic function references the variable created by the method, The point of this is the object that the method belongs to;
1 varMyObject =NewObject ();2Myobject.value = 0;3 4 functionAdd (num) {5 returnNum *num;6 }7MyObject.Double=function (){8 varhere = This;//points to the object to which the current method belongs;9 Ten varHelper =function (){ One varthat = This;//pointing still to window; AThat.value = Add (50);//window.value = 2500; -Here.value = Add (100);//myobject.value = 10000; - } the Helper (); - } - -MyObject.Double(); +Document.writeln ((Myobject.value));//10000; -Document.writeln ((Window.value));//2500;
constructor invocation Pattern
A function that, if created to be called by a new prefix, is called a constructor function, and, by convention, they are stored in a variable named in uppercase in the first letter. If you call the constructor function without adding new in front of you, it can be a very bad thing, with neither compile-time warnings nor runtime warnings, so the capitalization conventions are important ! (It is not recommended to use this form of the constructor, the next chapter has a better alternative [JS language 30 pages]);
Apply Call pattern
Because JS is a functional object-oriented programming language, functions can have methods.
The Apply method lets us construct a parameter array to pass to the calling function, which also allows us to select the value of this. The Apply method receives two parameters, the 1th one is the value to bind to this, and the 2nd is an array of arguments. 1 function Add (NUM1, num2) {
2 returnNUM1 +num2;3 }4 5 //constructs an array containing two numbers and adds them together;6 varArray = [3, 4];7 varsum = add.apply ( This, array);8 alert (sum);9 Ten One varQuo =function(string) { A This. Statues =string; - } - the //provide a public method named Get_statues for all instances of Quo; - -Quo.prototype.get_statues =function (){ - return This. Statues; + } - + varStatuesobject = { AStatues: "A-OK" at }; - - varStatues = Quo.prototype.get_statues.apply (statuesobject);//The statues value is "A-OK";
26
I haven't figured out the above code--
Arguments parameters
When a function is called, it gets a "free" distribution parameter, which is the arguments array, which the function can use to access all the argument lists passed to it when it is invoked.
Because of a design error in the language, arguments is not a real array, it is just an "array-like (Array-like)" object. Arguments has a length property, but it does not have any array of methods. We will see the result of this design error at the end of this chapter (Language essence P30);