Javascript object-oriented programming function is a method (function)

Source: Internet
Author: User

During programming, you must inevitably encounter complex functions. Beginners are most afraid of complicated functions. because they cannot well divide functional boundaries, they can only stack a large string of if, loop, and case. As a result, the program looks dizzy, others look dizzy. A good program is not written to computer, but to human. When encountering complex functions, we should consider simplifying and componentization of them and encapsulating small functions into small components. Small functional blocks can be combined to obtain ever-changing and complex functions. Function can help us encapsulate the function. What is encapsulation. I want to say that as long as the specific implementation is packaged and the calling interface is provided externally, it is encapsulation, and the methods and classes are also done.

Functions in javascript can be used to create methods or classes. In fact, we can think of them as classes simulated by functions. (when it comes to classes, we generally need to understand the knowledge of closures ). Let's take a look at the method first.

Javascript Functions include famous functions, anonymous functions, and immediate execution functions extended based on anonymous functions.

A common function is a famous function directly declared by a function.

Function Hello () {alert ("hello, everybody! ") ;}; Hello (); function SayHelloTo (somebody) {alert (" hello, "+ somebody + "! ") ;}; SayHelloTo (" James "); the Hello and SayHelloTo methods are created. Hello is called directly through Hello () without parameters. The SayHelloTo method has a parameter. WHEN greeting someone, you need to know who is greeting. When SayHelloTo ("Zhang San") is called, a parameter is required. These codes are similar to java and C. There is a big change in method overloading. javascript itself does not support any overloading. A method name corresponds to a method. If multiple methods with the same name are forcibly written, the first method will be overwritten.

Function Hello () {alert ("hello, everybody! ") ;}; Hello (); function Hello (somebody) {alert (" hello, "+ somebody + "! ") ;}; Hello (" James ");

The first Hello method is overwritten. If you call Hello () directly during execution, the second Hello method is called but no parameter value is passed. Therefore, the undefined information is displayed. When you call Hello ("James"), the execution is completed normally. In fact, javascript can also be overloaded in some straightforward ways. Anyone who has learned C # will know that there is a params keyword, which can pass an indefinite number of parameters to the method. We can manually judge the parameter information and simulate the effect of similar overloading. In javascript, if you do not need any params keyword, You can naturally transfer any number of parameters. Function has an arguments attribute. You can think of it as an array, which stores all parameters in the order of passed parameters. That is to say, we can not declare the parameter name when defining the method.

Function ShowArguments () {var args = ""; for (var I = 0; I <arguments. length; I ++) {args + = arguments [I] + "," ;}; alert (args. substr (0, args. length-1) ;}; ShowArguments (1, 2, 3, 4, 5, 6, 7 );

Try to use argements to simulate the overload.

Function Hello () {if (arguments. length = 0) {alert ("hello, everybody! ");} Else {alert (" hello, "+ arguments [0] + "! ") ;}}; Hello (); Hello (" Zhang San "); based on the number of parameters.

Function Increase (arg) {if (typeof arg = "undefined") {alert ("Enter the parameter");} if (typeof arg = "string ") {alert (String. fromCharCode (arg. charCodeAt (0) + 1);} if (typeof arg = "number") {alert (arg + 1) ;}}; Increase (); increase ("a"); Increase (1); Based on the overload of different parameter types.
Functions can also be anonymous functions apart from well-known functions. Anonymous functions are functions without sub-names. They are all complete function objects, no matter whether they are well-known or have no sub-names. Anonymous functions are declared using functions, but they do not need to be specified. Other aspects, such as parameters, are no different from those of name functions.

Function (){...... }; Anonymous functions can generally meet the needs of temporary functions and do not need to be referenced by variables (a famous function can be considered as a function with variable reference ). For example, you can use anonymous functions to add events to an object by using a function as a value object as a parameter input method and programming method. Of course, you can declare variables separately to reference an anonymous function object, which is no different from a common famous 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]; each (nums, function (arg) {alert (arg) ;}); execute the code above and output the elements in the array in sequence.

// When loading the form, the current time window is displayed on the title. onload = function () {document. title = new Date (). toString () ;}; // You can also pass the anonymous method to setInterval (function () {document. title = new Date (). toString () ;}, 1000); use an anonymous function to bind events and perform scheduled operations.

Var Hello = function () {alert ("hello, everybody! ") ;}; If an anonymous function is assigned to a variable, it is no different from a normal function with a name. However, whether it is a variable reference or a common well-known function, such a function occupies a certain amount of resources permanently in the memory. Sometimes we only want to execute a function that does not need to be referenced. directly executing an anonymous function may be the best choice. Wrap the anonymous function and add a bracket for execution. Everything is OK. This is the immediate execution function extended by the anonymous function.

(Function () {alert ("hello, everybody! ") ;}) (); (Function (somebody) {alert (" hello, "+ somebody + "! ");}) (" Zhang San "); immediate execution of the function often has unexpected results in event binding and callback function setting, and can solve such problems as object reference.

Var student = {Name: "James", Age: 20, Introduce: function () {alert ("My Name is" + this. name + ", this year" + this. age + "years old! ") ;}}; Window. onload = (function (obj) {return function () {obj. introduce () ;};}) (student); because of these features of functions in javascript and the features of its objects, we can also write programs with functional meanings. In fact, functions in javascript are 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;}, 100); what is next? Is it a method? Is it a class?

Function Point (){};

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.