Do you know functions in JavaScript?

Source: Internet
Author: User
Tags define function

As one of the core technologies of Javascript, Function has a clear understanding of function mechanisms and usage, which is very important for javascript development. today, I will introduce some simple usage of functions in javascript based on my own practices.

1. What is a Function?

From the definition of function in Javascript, function is an object composed of a collection of codes.

From this we can see that we can use functions as functions in C language, or object-oriented programming for functions. Of course, the function in Javascript is strong.

More than that.

2. How to Use function

2.1 Definition

 
 
  1. function myfunc(param) {  
  2.     //code  

Note that the two functions in Javascript are considered to be the same:

Which function is called at runtime depends on the loading order, and the latter function will overwrite the previous one.

 
 
  1. function func1() {return 'func1'; }  
  2. function func1(name) { return name; } 

In other words, function parameters are all optional parameters. Therefore, funciton recognition does not include parameters such as parameters. function input parameters are declared for reference convenience and readability.

The above code is also equivalent:

 
 
  1. function func1() {  
  2.     return arguments[0] || 'func1';  
  3. }  
  4. func();              //return 'func1'  
  5. func('function'); //return 'function' 

2.2 function as an object

Yes. In javascript, function is an object. We can use function as an object.

It can have its own attributes and methods. There is a funciton as follows:

 
 
  1. function nameOf(name) {  
  2.     return name.toUpperCase();   

2.2.1 function value assignment as an object

 
 
  1. var person = person || {};  
  2. person.nameOf = nameOf;  
  3. person.nameOf('yang dong') // return "YANG DONG" 

2.2.2 define function Attributes

Let's take a look at the following code:

 
 
  1. function nameOf() {return nameOf.blogger;}  
  2. nameOf.blogger = "YANG_DONG"; 

Yes, a function can have its own attributes.

Consider this scenario. If we want to count the number of times a function is called, there are two ways to achieve this:

1. Set a global variable to record the number of times the funciton is called. For each call, add 1 to the variable:

 
 
  1. var globalNameOfCounter = 0;  
  2. nameOf();  
  3. globalNameOfCounter ++; 

This seems to be okay. When the code is still relatively simple, it can work well, but as the Code becomes more complex, the cost of maintaining this logic will soar.

It is mainly because: globalNameOfCounter pollutes the global namespace and destroys code encapsulation.

2. Use function Attributes

Take a look at the following code:

 
 
  1. function nameOf() {  
  2.     nameOf.counter++;  
  3.     return nameOf.blogger;  
  4. }  
  5. nameOf.blogger = “YANG_DONG"  
  6. nameOf.counter = 0;    
  7. nameOf(); //nameOf.counter = 1  
  8. nameOf(); //nameOf.counter = 2 

Obviously, the second method has good encapsulation and maintainability. More applications with function attributes are available. See the following.

2.3 function as namespace

Javascript does not support namespaces (I do not quite understand why such a powerful language does not support namespaces. Why ?),

However, we can still use its powerful funciton to support namespace.

From the above section, we know that a function can define its own attributes, and we can use this feature to generate a namespace. Please refer to the following code:

 
 
  1. nameOf.getBloggerName = function() {  
  2.     return nameOf.blogger;  

In this case, the nameOf namespace already contains: blogger, counter attribute and function getBloggerName.

2.4 function as method

In javascript, function and method have no essential difference. If we do not distinguish them, I think this variable is different.

 
 
  1. Function g () {return this ;}
  2. Var local = local | {};
  3. Local. method = g; // modify this to point to local
  4. Local. method (); // returns the local object
  5. G (); // return the DOMWindow object

2.5 all functions are closure

All functions in Javascript are bound with a scope chain, so it is a function that saves the call context. Let's take a look at the following instance code:

 
 
  1. var variable = 'global';  
  2. function getVariable(){  
  3.     var variable = 'local',  
  4.     func = function() {  
  5.         return variable;  
  6.     };  
  7.     return func;  
  8. }  
  9. getVariable()();  //return local; 

When a func is called, The varvisible value is a variable in the call context, not a global variable with the same name.

3. Summary

To summarize the introduction to funciton in one sentence, I think the function is a code set object that can be called and executed.

The above are some application scenarios of function, of course it is more than that.

For example, a function is used as a constructor (that is, funciton is used as a class) and inherited.

Original article: http://www.cnblogs.com/yangdong/archive/2012/02/05/function-in-javascript-0.html

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.