The path of JavaScript design pattern Learning--object oriented thought

Source: Internet
Author: User

Today, I got the book "JavaScript design Mode" written by Zhang Jongming, and began a deeper study of JavaScript.

See the beginning of the book, although before reading, some good video of the explanation, to JavaScript has a basic understanding, but, in the content of the book, I still have a feeling of their own poor 108,000, the previous code is not written, using global variables, global functions, is just a process-oriented approach to programming, which is too extensible and reusable. In the future I will update the blog every day, I will learn each of the design patterns are po out, I want to see their own progress!! Come on!

Today's content is ———— object-oriented thinking

The main idea: should discard the previous process-oriented thinking and code writing, minimize the use of global variables, global functions. In conjunction with the other "high-performance JavaScript" I saw today, for JS performance, global variables and the scope chain of variables in the DOM are located under the local variables, that is, the search order, the first to find the local variables, and then find the global variables. Therefore, the object-oriented thought should be adopted for the demand of performance, the cooperation of team and the reusability of code.

Case: Check the name, mailbox, password compliance specifications, detailed check function omitted.

Method One:

1     //Wrap the method you want to add with the object2     //Method 1: Use the JSON method to encapsulate a function, the disadvantage of not using new, the extensibility is not strong3     varCheckobject = {4CheckName:function(){5             //Check Name6Alert ("CheckName");7             return  This;//Call for chaining8         },9Checkemail:function(){Ten             //Check if email is legal OneAlert ("Checkemail"); A             return  This;//Call for chaining -         }, -Checkpassword:function(){ the             //Check Password -Alert ("Checkpassword"); -             return  This;//Call for chaining -         } +     }; -  + Method One Call ACheckobject.checkname (). Checkemail (). Checkpassword ();

Method Two:

I think that a better method, combined with my own class invocation habits, in the function of the prototype to add methods, to avoid the pollution of the native object function, create a unified method for it to add methods, when they add methods, for the use of class calls, The method in which it is added is in its prototype.

1Function.prototype.addMethods =function(NAME,FN) {2          This. prototype[name] =fn;3         return  This;4     }5     varMethods =function(){};6     //var Methods = new Function ();7Methods.addmethods (' CheckName ',function(){8         //Check Name9Alert ("CheckName");Ten  One}). Addmethods (' Checkemail ',function(){ A         //Check if email is legal -Alert ("Checkemail"); -  the}). Addmethods (' Checkpassword ',function(){ -         //Check Password -Alert ("Checkpassword"); -  +     }); -  +     //when method two is called A     varm =NewMethods (); atM.checkname ();

Hope you have a better opinion at any time reply, if wrong, please advise!

The path of JavaScript design pattern Learning--object oriented thought

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.