JS OOP Package

Source: Internet
Author: User

Good reader friends, before we have explained the basics of JavaScript, starting with today's content, we are going to start to talk about the content of the package, here, we have 1.1 point of contact with OOP (object-oriented programming), If the programmer used as a language does not know what the OOP is, or just heard that they do not understand, can not write object-oriented code, then there is no need to learn the program, the following I will give you a detailed introduction of what the object-oriented, the process is what, to understand the object-oriented, First, we have to know the process-oriented. After you've figured out the process, we can't blindly oop for OOP, that's just doing something in vain, because most of the time we write code just to solve a small thing, then we don't have to write oop, just write some process-oriented code,  Use a word to describe "local conditions". Let's start with the process, first to explain what the process means, we usually call the "program" in fact the process, the implementation of a program, that is, to perform a process, for example: To work, is a program, wherein the process is, 9 o'clock on time to the company, to do some of their own should be done, 6 o'clock in the afternoon no matter what it is, the process of working is done, leaving the company, this is the process; another more specific example, withdrawals, the process of execution we write more clearly:1, bring your bank card to the ATM2, insert a bank card3, enter a password4, enter the number of withdrawals5, ATM spitting6and put the money in your pocket6, if you find that you don't have the number you want, return to the 4th step7, if you want to print receipts, click Print Receipts, or omit this step if you do not print8, return card9, program finished from the above steps we can see, from the first step to the last step, this is the execution sequence, 4th to 6th step is a loop process, 7th step is a branching process, this is the process, that is, the program. The purpose of our code is to simulate some behavioral processes, using the high-speed computing characteristics of computers for our life services. We can wrap the withdrawals into a function, so that this is a separate process, we can call this function when needed, we can complete our work needs, below we use one of the simplest examples to express a simplest program procedure copy code function Kisswife ( Whosewife) {Console.log (Whosewife+"put your face together."); Console.log ("I'm going to print my lips up ."); Console.log ("Wood, a sound"); Console.log ("Pro"+whosewife+"once completed");} Kisswife ("my wife"); Copy the code look at the diagram to speak, at the right time, we call the Kisswife function, enter the appropriate parameters, we execute a process. The goal of OOP is to improve the rate of reuse of code, with a minimum of code to do as much as possible, the use of parameters, but also a manifestation of object-oriented programming, we have to give a counter example, if we do not use parameters, we want to kiss someone else's wife, it is necessary to re-write a kisswife function, so, We wrote a lot of repetitive code, inconvenient code management, a lot of inconvenience, the method is not Shunliu, even in the kiss other wife when the chance of being found is greatly increased, brought some unnecessary trouble. At this time there are students want to ask, I think, I feel I am not accustomed to using parameters, is not to pass parameters, code management where will appear inconvenient management? Well, the question is pretty much in place. Let me explain, if, during the course of the process, we find that there are unreasonable places that need to be modified, for example, if I want to extend a tongue, we have to modify (trouble) in the function of kissing our own wife, and in the function of kissing someone else's wife (trouble+1), when we have a lot of similar functions, is not to make all the changes (trouble +n); the second disadvantage is the number of changes, you can guarantee that all the changes will not be wrong (easy error), this can be reflected, if we just completely write some repetitive code, the efficiency is greatly discounted. In the above explanation, the Novice readers still do not understand what is OOP (object-oriented programming), we are now from the object (Objectstart to explain, the object here, can not simply understand the love of the men and women in the friend, object refers to the world everything, the sun, sea, people, pets ... , every thing we can want, every object has its own properties, behavior. We can understand, like this, that a bird is an object, has its own properties, has its own behavior, and we use specific code to encapsulate a class of birds. (Note: In the JavaScript language, the function keyword is used only to define functions, you can also define a class, it does not use the class keyword like a high-level language, and later we say inheritance, we also use special methods to implement inheritance) copy code//Declare a birdfunction Bird () { This. Name ="Pigeons";  This. color ="Grey";  This. Habitat ="Cages";  This. Weight ="500 grams";}//Use the prototype chain to define the row of a bird, or to define a property, but the property is generally declared with the This keyword//behavior and attributes, in fact the same level, after we use for in to give you a verification//TweetBird.prototype.Sing =function () {Console.log ("Goo");}//EatingBird.prototype.Eat =function () {Console.log ("ate a grain of corn");}//FlyBird.prototype.Fly =function () {Console.log ("flying in the sky");}//Hatching EggsBird.prototype.Brood =function () {Cossole.log ("Hatching pigeon Eggs");} Copy the code now that our class has been declared, but how do we use it? Now it's just a class, not an instance, is what I said in the words of pigeons, example, is a specific pigeon, how to get a specific pigeon? Look at the code below.//Use the New keyword to get an instancevarGezi =NewBird (); Now we can invoke its properties, as well as how we get every pigeon in this way is actually the same, how do we get pigeons with their own characteristics? have their own unique characteristics, is actually the property is not the same, then we will change the function declaration copy code//Actually, we just need to change it here a little bit .function Bird (_color,_habitat,_weight) { This. Name ="Pigeons";  This. color =_color;  This. Habitat =_habitat;  This. Weight =_weight;} Copy the code and then we'll take a look at what to do when we instantiate a pigeon.//we've instantiated two pigeons right now.varGezi_a =NewBird ("White","Wild","300 grams");varGezi_b =NewBird ("Grey-White","Greenhouse","550 grams"In this way, we can construct a pigeon with its own characteristics come out, from the above example, we are actually not difficult to see, encapsulation, in fact, we can describe the object with a class to represent, we can use the New keyword to instantiate the object, the object has its own independent properties, behavior, Such an object, we can be convenient for us to operate, encapsulation is a way to embody the OOP, we first encapsulate a class, then, and then new out of the instance, so write more than we directly in the code to construct two times the pigeon Class A lot less code, if we also construct the 3rd pigeon, it will be new again. , when you construct the object, you feel a code thing.  To improve the reuse rate of code, OOP is reflected in this. At this point, someone asked, just to say that we use the advantage of OOP, have not yet seen how to write the code without OOP, they come to a way without OOP, also use pigeons as an example to copy code//Declare a pigeonfunction Gezi_c () {Console.log ("species are pigeons"); Console.log ("color is blue"); Console.log ("live in the treetops."); Console.log ("weight 400 grams"); Console.log ("flying in the sky");}//Execute onceGezi_c (); Copy the code if we are going to declare 100 more pigeons, is not to write a lot of repetition like the above code ah, this is the process-oriented code. I believe that the novice friends have a vague concept of OOP, slowly experience, this feeling can be understood in two days, a bite to eat not big fat, and then we will continue to talk about the inheritance and polymorphism of OOP ideas. Continuing to declare the bird class before, the property and behavior are the same level, and can be declared in two ways, the property is used in the constructor Thiskeyword declaration, the behavior function is declared by the prototype keyword, prototype is the standard extension of the function prototype chain, we write this is to use JavaScript language into the category of high-level language, to simulate the use of high-level language, pull away, Let's start by verifying that the bird instance object does not have the same level of properties as the behavior function copy code//We are now using the bird class with no arguments in front ofvarobj =NewBird ();//print them out individually for(varProinchobj) {Console.log (pro+" : "+Obj[pro]);} Copy the code, you see . for... in the function is used to iterate over the properties of the object and array subscript, the name of the behavior function is actually the property of the object, now verify the previous statement, I believe that we now have a certain understanding of the packaging of OOP ideas. To summarize, what we are talking about today is actually the abstraction of things, then, to encapsulate these attribute behaviors into a class, using the new keyword to instantiate a specific object, which greatly improves the usage of the code and improves the efficiency

JS OOP Package

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.