Big talk JS Object-oriented Beginning everything is Object------(the thinking of ATM machine)

Source: Internet
Author: User

One, general overview

The concept of OO (object-oriented) is a revolution in the development of software development engineering, which has enabled many large-scale applications to be implemented smoothly over the years. If you are not yet mastering and using OO for programming and development, you will undoubtedly stay in the Stone Age of software development. Most programming languages, especially those that come out in recent years, are well-supported for object-oriented, and you might be at the helm, but some languages are less difficult to object-oriented programming and development in OO terms than other high-level languages, such as the JavaScript discussed in this article. JavaScript is an ancient language, but with the recent Web2.0 technology, the language has renewed its youthful glow, with the help of JavaScript client technology, our web experience has become rich and exciting, in order to design and develop more sophisticated, complex client applications, We have to master the OO approach on JavaScript, which is what this article is going to discuss.

Today's JavaScript is a great way to rely on a wide range of applications. Web programmers have become accustomed to using a variety of excellent JavaScript frameworks to quickly develop WEB applications, ignoring the learning and deep understanding of native JavaScript. So, often the case is that many of the programmers who have done it for many years are always unclear about closures, functional programming, and prototypes, even if the framework is used, and the code is poorly organized. This is an expression of insufficient understanding of native JavaScript language features. To master JavaScript, the first thing is to discard some other high-level languages such as Java, C # and other classes of object-oriented thinking of the interference, a comprehensive view of the functional language from the perspective of JavaScript prototype object-oriented features. With this in mind, it is possible to use the language better. This article is suitable for groups: programmers who have used the JS framework but do not understand the nature of JS language, have Java, C + + and other language development experience, ready to learn and use JavaScript programmer, and always to JavaScript is object-oriented ambiguous, but want to know the truth of JS enthusiasts 。

To the point, we cut into the subject------JavaScript Object-oriented programming. To talk about JavaScript's object-oriented programming, the first thing we need to do is forget about the object-oriented programming we've learned. Traditional C + + or Java object-oriented thinking to learn JavaScript object-oriented will bring you a lot of confusion, let us forget what we have learned, and start learning this special object-oriented programming. Since is OO programming, how to understand OO programming, remember to learn Java, learned for a long time not to get started, and later had the privilege to read the "Java programming thought" this masterpiece, suddenly enlightened, so this article will also be the object model to explore the JavaScript OO programming. Because of the specificity of the JavaScript object model, the inheritance of JavaScript is very different from traditional inheritance, and because there is no class in JavaScript, it means that there is no extends,implements in JavaScript. So how does JavaScript actually implement OO programming? Well, let's start with a walkthrough in JavaScript's Oo world.

Second, the introduction of the case

(1) Introduction of scene characters

Hero a------Big ear teacher (the Software industry field of the ash-level program Ape)

Hero b------Big Xun Jun (rookie novice programmer)

Relationship------The upper and lower of the work, the private apprentice

(2) Topic development

One day this apprentice two people chatting up after lunch, suddenly chat atm, big ear teacher said Teller machine to People's daily life help is really great, but also improve the bank's productivity, this invention and our software industry is inseparable, big Xun Jun said yes teacher, some practical operation provides convenient function, Then the big ear teacher smiled and said, "then you know how these functions are realized, full of confidence in the Big Bear replied:" Simple ah is some like saving money, to withdraw funds, the simple function of transfer, the teacher I write to you now see .... Not to be continued

(3) Example explanation, gradual

10 minutes later, Big Bear June put a copy of the code of the operation part of the teacher to see, the teacher smiled (hehe)

1 functiontransfertransaction (fromaccount,toaccount,balance) {2      This. Fromaccount =Fromaccount;3      This. Toaccount =Toaccount;4      This. Balance =balance;5 } ;6Transfertransaction.prototype = {7Transfer:function(){8          This. Toaccount = This. Fromaccount-balance;9     } ,TenGetfromaccount:function(){ One         return  This. Fromaccount; A     } , -Gettoaccount:function(){ -         return  This. Toaccount; the     } , -GetBalance:function(){ -         return  This. Balance; -     } + } ; -  + vartt =NewTransfertransaction (1000,3000,100) ; A Tt.transfer (); atTt.gettoaccount ();
View Code

The teacher said Big Bear you this code is problematic, the big Bear did not think too much direct answer said, no AH teacher I this is object-oriented write Ah, the teacher very patient said: Don't worry to listen to me to tell you first.

First of all, do not say that the object-oriented function is also problematic, assuming that my transfer account is fromaccount if the balance of 0 that, will not be the problem, Big Bear, Big Bear looked at a bit very embarrassed to say AO is ah you wait a moment Big bear go back and make changes .... Not to be continued

I'm done with this.

1 functiontransfertransaction (fromaccount,toaccount,balance) {2      This. Fromaccount =Fromaccount;3      This. Toaccount =Toaccount;4      This. Balance =balance;5 } ;6Transfertransaction.prototype = {7Transfer:function(){8         if( This. Fromaccount <balance) {9             Throw NewError ("Insufficient balance!")) ;Ten         } One          This. Toaccount = This. Fromaccount-balance; A     } , -Getfromaccount:function(){ -         return  This. Fromaccount; the     } , -Gettoaccount:function(){ -         return  This. Toaccount; -     } , +GetBalance:function(){ -         return  This. Balance; +     } A } ; at  - vartt =NewTransfertransaction (1000,3000,100) ; - Tt.transfer (); -Tt.gettoaccount ();
View Code

The teacher looked at the basic function is to do, but this design is very inefficient, and a lot of design principles are also violated, we go back to the object-oriented, your code writing method does conform to the characteristics of object-oriented, but the idea still stay in the process of design ideas.

At this time, the Big bear June came very modest said please teacher guidance ... Not to be continued

A few minutes later the teacher summed up some examples of the problems

1. Responsibilities need to be decomposed

2. Abstract Entity Model

Big talk JS Object-oriented Beginning everything is Object------(the thinking of ATM machine)

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.