JavaScript object-oriented programming function is a method (function) _js object oriented

Source: Internet
Author: User
Tags anonymous current time
A good program is not written to computer, but to human. Encounter complex functions, you should think of it simplified, modular, small functions packaged into groups, small function blocks can be arbitrary combination of the ever-changing complex functions. function can help us to encapsulate functionality. So what is encapsulation? I would say, as long as the specific implementation of packaging, external provision of the calling interface that is encapsulation, methods or classes are doing these things.

The function in JavaScript can be used to create a method, or it can be used to create a class, and we can actually think of a class that simulates it with a function (when it comes to classes, the knowledge of closures is generally understood). Let's take a look at the method first.

JavaScript functions are divided into well-known functions, anonymous functions, and immediate execution functions that extend on the basis of anonymous functions.

A common function is a well-known function that is declared directly with function.

 function Hello () { 
 alert ( "Hello, everybody!");  
 
   
 Hello (); 
   
 function Sayhelloto (somebody) { 
 alert (  
 
   
 Sayhelloto ( "John");  

      the Hello and Sayhelloto methods are created separately. Hello does not have parameters and is called directly through Hello (). The Sayhelloto method takes a parameter, and the greeting to whom you need to know is the greeting. The arguments are passed in when Sayhelloto ("John") is invoked. These codes are not much different from Java or C #. There is a large change in the method overload, and JavaScript itself does not support any overloads, and a method name corresponds to a method. If you force to write multiple methods of the same name, there is a situation where the first write method is overwritten.

 function Hello () { 
 alert ( "Hello, everybody!");  
 
   
 Hello (); 
   
 function Hello (somebody) { 
 alert ( "Hello"
 , "+ Somebody +   
 
   
 Hello ( "John");  

The first Hello method is overwritten, and a direct call to Hello () while executing is that the second Hello method is invoked but no parameter value is passed, so the undefined information pops up. When you call hello ("John"), the execution is done normally. In fact, JavaScript can also use some straightforward way to complete the overload. Anyone who has learned C # will know that there is a params keyword through which you can pass an indefinite number of arguments to a method. We can use the information of the parameter to make the manual judgment also can simulate similar overload effect. And in JavaScript does not need any params keyword at all, can very naturally realize any number parameter transmission. There is a arguments attribute in the function that you can think of as an array that holds all the parameters in the order of the arguments passed in. That means we can define a method without declaring the parameter name.

    function showarguments () {
      "";
      for (var i = 0; i < arguments.length; i++) {
        ",";
      };
      Alert (args.substr (0, args.length-1));
    };
 
  
    Showarguments (1, 2, 3, 4, 5, 6, 7);


Try using argements to simulate the overload.

 function Hello () { 
 if (arguments.length = = 0) { 
 alert ( "Hello, everybody!");   
} 
 else { 
 alert (  
 
}; 
&NBSP; 
 Hello (); 
 Hello ( "John");  

      overloads based on different number of parameters.

 function Increase (ARG) { 
 if (typeof arg = =  "undefined") {   
 alert ( "Please enter parameters");  
} 
 if (typeof arg = =  Stri Ng ") {   
 alert (string.fromcharcode (arg.charcodeat (0) + 1)); 
 
 if (typeof arg = =  numb Er ") {   
 
} 
 
 increase (); 
&NBSP; 
 Increase ( "a");  
 increase (1); 
Overloads that are based on different parameter types.

Functions can be anonymous functions in addition to well-known functions, anonymous functions are no names of functions, whether the function is well-known or no name, are a complete function object. The anonymous function is also declared with a function, but no name is specified for it. Other aspects, such as parameters, are no different from well-known functions.

    function () {
      ......
    };

Anonymous functions can generally satisfy temporary function requirements and do not need to be referenced by variables (well-known functions can be considered to be functions that have variable references). For example, the need for a function as a value object as a parameter to pass the method, need to programmatically add events to the object, with anonymous functions can be done well. Of course, you can also declare variables to refer to an anonymous function object, which is no different from the common well-known function.

    function each (array, fun) {
      for (var i = 0; i < Array.Length; i++) {
        Fun (Array[i]);
      };
    };
    var nums = [1, 2, 3, 4, 5, 6, 7];
    Function (ARG) {
      Alert (ARG);
    });

The code above executes, sequentially outputting the elements in the array.

 //when the form is loaded, display the current time on the caption  
 window.onload = function () { 
 Document.title = new Date (). toSt
 Ring ();  
}; 
&NBSP; 
 //can also pass anonymous methods into the timer  
 SetInterval (function () { 
 document.title = }, 1000); 

Use anonymous functions to bind events and to perform timed operations.

    function () {
      Alert ("Hello, everybody!");
    };

If you assign an anonymous function to a variable, it is no different from a well-known normal function. However, whether it is a variable reference or a commonly known function, such a function in memory for the persistence of a certain resource. Sometimes we just want to do it once. It is probably the best option to execute anonymous functions directly without using a referenced function. Wrap the anonymous function in parentheses, all OK, this is the immediate execution function extended by the anonymous function.

function () { 
 alert ( "Hello, everybody!");  
 
   
function (somebody) { 
 alert ( "Hello,"
 + Somebody +  "!");   
 "John");  

The immediate execution function in the event binding, set the callback function and so on often has the unexpected effect, can solve such as the object reference and so on.

 var student = { 
 Name:  "John",  
 AGE:20, 
 introduce: function () /span> 
 alert ( "my name" + this. Name + this. Age +      
}}; 
 Window.onload = (function (obj) {return    

Because these features of functions in JavaScript plus the characteristics of its objects, we can also write some functional-meaning programs. In fact, the function of JavaScript is really the boss.

 function Sum (fun, x) { 
 if (x <= 0)  
 return 0;  
 return Fun (x) + Sum (fun, x-1);  
 
   
 alert (Sum (function (i) {return i * I;});   

What's next? Is it a method? Is it a class?

    function Point () {
      
    };

First long-winded to this, next time to look at the class.

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.