Basic concepts of javascript--objects (II.)

Source: Internet
Author: User

First, create a function

A function is an object: A function class is an object that can be instantiated by functions, but most of it is using function to create functions.

Method One: Use function classes to instantiate functions:

1 var people=New Function ("name", "Sex", "if (sex== ' m ')"  return  name+ ' Mr.: Hello! ';  else   return name+ ' Lady: Hello! ‘;" ); 2 people (' Xiao Qiang ', ' Man ')    //' Mr. Xiao Qiang: Hello! "
function class instantiation functions

Syntax format: var function_name = new Function (arg1, arg2, ..., ArgN, function_body)

    • The first n of the parameters are the formal parameters of the function, and the last is the function body of the function (the executing part);

mode two: Use function to create functions, The syntax is as follows:

var a= function (parameter 1, parameter 2 ...) {}

or function A (parameter 1, parameter 2 ...) {}

Cases:

1     function people (Name,sex) 2         {3             if (sex== ' m ')4                   return name+ ' Mr.: Hello! '; 5             Else 6                 return name+ ' ladies: Hello! '; 7         }
Create a method

Note the following issues:

    • When a parameter is set and the parameter is forgotten, the JavaScript engine automatically passes a undefined parameter;
    • When the passed parameter exceeds the set number of parameters, the extra part will be ignored automatically;
    • JavaScript uses an arguments object to automatically manage the parameters that are accepted by the function, which resembles an array.

As can be seen from the above two ways, the function created is ultimately the same, it is important to remember that a point function is an object, which is a reference type.

Second, the variables in JavaScript

The variables in JavaScript are divided into local variables and global variables.

1, local variables: variables declared within the function of Var, can only be accessed within the function, the function will delete the local variables;

2. Global variables:

    • Variables declared outside of a function are global variables, and all scripts and functions on a Web page can access it.
    • Variables declared within a function that are not using VAR are also global variables, such as carname= "Volvo"; but this global variable can only be accessed globally after executing the function.

Third, function-related properties:

1, arguments: Access function input parameters, automatically created by the script interpreter, with the length property, similar to an array, you can use an array of methods to access each parameter.

Example 1:

1     functionpeople (Name,sex)2         {3             4             if(sex== ' Male ')5                   returnname+ ' sir: Hello! A total of ' +arguments.length+ ' parameters ';6             Else7                 returnName+ ' Lady: Hello! A total of ' +arguments.length+ ' parameters ';8         };9People (' Xiao Qiang ', ' Male ');//"Mr. Cockroach: Hello!" A total of 2 parameters "TenPeople (' Xiao Li ', ' female ');//"Ms. Li: Hello!" A total of 2 parameters "
arguments Use Example 1

Example 2:

1     functionpeople (Name,sex)2         {3             4             if(sex== ' Male ')5                   returnarguments[0]+ ' sir: Hello! A total of ' +arguments.length+ ' parameters ';6             Else7                 returnarguments[0]+ ' Lady: Hello! A total of ' +arguments.length+ ' parameters ';8         };9People (' Xiao Qiang ', ' Male ');//"Mr. Cockroach: Hello!" A total of 2 parameters "TenPeople (' Xiao Li ', ' female ');//"Ms. Li: Hello!" A total of 2 parameters "
arguments use Example 2

Example 3:

Results

1         functionsum (v1,v2)2         { 3Document.writeln ('////////////////////////////////////////////////////////////////////////');4Document.writeln (' <br> call parameter:<br> ')); 5Document.writeln (' Parameters have ' +arguments.length+ ':<br/> ');6             varS=0;7              for(vari=0;i<arguments.length;i++) 8             { 9Document.writeln (' parameter ' +i+ ': ' +arguments[i]+ ' <br/> ');Ten             }  One              for(vari=0;i<arguments.length;i++)  A             {  -s=s+Arguments[i]; -             }  theDocument.writeln (' Parameters ' and: ' +s + ' <br/> '); -Document.writeln ('////////////////////////////////////////////////////////////////////////'); -};
Arguments use Example 3

As can be seen from the example, the arguments object operates only on arguments, not formal parameters, so we can actually avoid having to set formal parameters, which is, of course, prescriptive or well-specified.

2. callee:arguments property, the table is executing the function object.

For example:

1     functionsum (n)2     {3             if(n<=0)4                    return0;5              Else 6                    returnN+arguments.callee (n-1);7     }8SUM (2);//39SUM (100);//5050
Arguments.callee Use Example

3. Calleer: The property of the function object, the parent function object of the table function object. function Foo1 (v1)

Cases:

Results:

1       functionfoo1 (v1)2{Foo2 (v1,v1*2);} 3       functionFoo2 (V1,V2)4{Foo3 (v1,v2,v2*2);} 5       functionFoo3 (V1,V2,V3)6       { 7           varfoo=Arguments.callee;8            while(foo&& (foo!=window)) 9 {TenDocument.writeln (' <br> call parameter:<br> '));  One             varargs=foo.arguments; Aargn=args.length; -document.write (' Parameters have ' +argn+ ': \ n '); -              for(vari=0;i<argn;i++)  the { -Document.writeln (' args[' +i+ '): ' +args[i]+ ' <br> ');  - } -foo=Foo.caller; + } -       }; +Document.writeln ('////////////////////////////////////////////////////////////////////////');
Arguments.caller Application Examples

4. Apply and Call methods: Bind functions to other objects to execute.

Apply: The parameter will be passed in as an array;

Call: Parameters are separated by commas and passed in as a list.

1     functionClassA ()2     {3           This. Name= ' ClassA ';4           This. methoda=function(msg) {return  This. name+ ': ' +msg};5     }6     functionClassB ()7     {8           This. Name= ' ClassB ';9           This. methodb=function(msg) {return  This. name+ ': ' +msg};Ten     } One     varObja=NewClassA (); A     varobjb=NewClassB (); -Obja.methoda (' Call MethodA ');//"ClassA: Call MethodA" -ObjA.methodA.apply (objb,[' call MethodA ']);//"ClassB: Call MethodA" theObjB.methodB.call (obja,[' call MethodB ']);//"ClassA: Call MethodB"
the use of apply and call

5. Length: Returns the number of parameters, which is the number of arguments expected when the function is defined, regardless of the number of arguments (actual arguments) that are actually called.

6, ValueOf (), toString (): Returns the code of the function

7, this

    • An instance of the newly created object in the constructor function table;
    • When a method of an object is called, it refers to an object instance that invokes the method;
    • Represents a Windows object when the function acts as a method instead of an object's method;
    • Represents a Windows object outside the body of a function;

Basic concepts of javascript--objects (II.)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.