Write at the front:
objects are just special data types with properties and methods (JS's 7 data types: String, number, Boolean, array, object, null,undefined).
JavaScript is an object-oriented language, but JavaScript differs from other object-oriented languages in that it has no concept of class . No class naturally does not create objects through classes, JavaScript is based on prototype, not classes.
All things in JavaScript are objects: strings, numbers, arrays, functions ... In addition, JavaScript allows custom objects to be customized.
JavaScript provides multiple built-in objects, such as String, Date, Array, and so on, starting with function.
Objects are special data types with properties and methods, starting with this sentence, looking at the function object from three aspects: 1, Declaration (data type) 2, attribute 3, method
I. Declaration 1, Function declaration
function foo (argname1,argname2,...) { body// do something }
2. Function expression
var function (argname1,argname2,...) { body// do something}
3. Function constructor
var New Function (argname1,argname2,..., body);
Properties (not including inherited attributes) 1, Arguments property
Gets The parameters (arguments object) of the currently executing Function object.
Example:
1 functionargtest (arg1, arg2) {2 vars = "";3s + = "The individual arguments is:"4 for(n = 0; n <Arguments.length; n++{//arguments is an object property, which is itself an object, with the length property5s + =Argtest. Arguments[n];6s + = "";7 }8 return(s);9 }TenConsole.log (Argtest ("Yuanliang", "Shuai"));
Output result: Theindividual arguments Are:yuanliang Shuai
2. Caller Properties
Gets the function that invokes the current function.
The caller property is defined only when the function is executing . If the function is called from the top level of the JavaScript program, then caller contains null.
Example:
1 function Callerfun () { 2 Console.log (callerfun.caller);//is defined only when the function is executing " 4 5 function init () { 6 Callerfun (); " 8 9 init (); 10 Callerfun ();
Output Result:function init () {callerfun ();} Null // The first call to Callerfun () is called by the Init function, so the INIT function is returned, and the second direct call returns null
3. Length Property
Gets the number of arguments for the function
When an instance of a function is created, the script engine initializes the function's length property to the number of arguments in the function definition.
Example:
1 function Lengthfun (ARG1,ARG2,ARG3,ARG4) {2 console.log ("Lengthfun ' s length=" +lengthfun.length); 3 Console.log ("Arguments ' length=" +arguments.length); 4 }56 lengthfun (n/a);
Output result:lengthfun ' s length=4 arguments ' length=3
As you can see from the example, the length of the function object and the length of the arguemnts are not necessarily the same.
Iii. methods (excluding methods inherited from Object objects) 1, apply () and call () methods
See previous post: http://www.cnblogs.com/wangyuanliang/p/4264505.html
2. Bind () method
For a given function, create a binding function of the same body as the original function. in a binding function, the thisobject resolves to an incoming object. The binding function has the specified initial parameters.
Function.bind (Thisarg[,arg1[,arg2[,argn]])
function
Must be selected. a function object.
Thisarg
Must be selected. The This keyword is an object that can be referenced in a new function. An object that
arg1[,arg2[,ArgN] ]
Optional. The list of arguments to pass to the new function.
Example 1:
1 functionBindfun (value) {2 if(typeofValue!== ' number ')3Console.log ("Not a number");4 Else5value>= This. min&&value<= This. Max?console.log ("number is correct!"): Console.log ("number is wrong!");6 }7 varoption = {Max:50,min:10};8 varJudgefun =bindfun.bind (option);9Judgefun (30);TenJudgefun (80);
Output Result: number iscorrect! number is wrong! The first returns correct within the range and the second does not return wrong
The parameter option here corresponds to thisarg, which is required .
Example 2:
1 function Bindparamfun (ARG1,ARG2,ARG3,ARG4) { 2 Console.log ("Bindparamfun params is:" +arg1+arg2+arg3+arg4); 3 4 var emptyobject = {}; 5 var bindParamFun2 = Bindparamfun.bind (Emptyobject, "a", "B" 7 bindParamFun2 ("C", "D", "E");
output:bindparamfun params are:abcd //In bindParamFun2 the first parameter is an empty object that acts as a thisarg, without this parameter, "A" will be treated as Thisarg and the output will be: Bindparamfun params are:bcde
Iv. Some places needing special attention 1, how to choose the way of declaration?
Which of the three ways should we choose to be appropriate in (a) statement?
Most JavaScript books do not recommend the use of a third, the third is a serious performance problem, this definition will parse two times the code, the first is the general parsing this statement, the second time to parse the function string into the actual function. The Third Way we can understandthis is that the function of J Avascript is actually an object, and the function name is the address to the object.
function declarations and function expressions:
These two methods do not have much difference in performance, but due to the difference in javasctipt preload order, you need to be careful when defining functions.
Before the code executes, the parser has read the function declaration and added it to the execution environment prior to the definition of the underlying underlying data type.
Example 1:
1 function Foo2 () { 2 console.log (" foo1 "); " 4 5 var fun = Foo2; function Foo2 () { 7 fun (); Console.log (" Foo2 " " 10 Foo2 ();
output Result:too much recursion // recursion occurs, memory overflow
If you change to:
Example 2:
1 var foo2 = function { 2 Console.log (" Foo1 " 3 }; 4 var fun = Foo2; 5 var Foo2 = function () { 6 Fun (); 7 console.log ("Foo2" ); 8 }; 9 Foo2 ();
Output result:foo1 foo2
The reason is that before the code executes, JavaScript defines the function in advance, namely: Funciton=>var
Compare
function Foo2 () {Console.log ("foo1");}; Old var foo2 = function () {Console.log ("foo1)}; Foo2 pointing to the old method
function Foo2 (fun (); Console.log ("Foo2")); New var fun = Foo2; Put the address of the Foo2 to fun, which points to the old method
var fun = Foo2; the address the fun points to is the new foo2 Foo2 = function () {fun (); Console.log ("Foo2)}; Foo2 points to the new method's address fun unchanged
Foo2 (); There will be a memory overflow foo2 (); Executive Foo2
2. No overloading
The function in JavaScript is not overloaded, and subsequent definitions will overwrite the previous definition.
1 function foo (arg1,arg2,arg3) {2 console.log ("This is a function:" +arg1+arg2+arg3); 3 }4function foo (arg1,arg2) {5 console.log ("Be Better:" +arg1+ arg2); 6 }7 foo ("Hello", "World", "Leon"); A method that does not have a zone to execute three parameters
Output result: bebetter:helloworld
Obviously, in the case of passing three parameters, the method defined later is still executed because of an overwrite instead of overloading. Equivalent:
1 var function (ARG1,ARG2,ARG3) {2 console.log ("This is a function:" +arg1+arg2+arg3); 3 }45function(arg1,arg2) {6 console.log ("Be Better: "+arg1+arg2); 7 }8
JavaScript Object (a)--function object