JavaScript Object-oriented programming (OOP) (a)-class

Source: Internet
Author: User
Tags class definition

Before you learn JavaScript object-oriented programming, you need to know and understand some of the basic common sense of object-oriented. Most of the beginners think that object-oriented programming is very important and takes up a great part of the effort. I have previously thought that OOP is the main object-oriented part, then I hold the same idea, the following first to correct this idea, really understand object-oriented.

First, the initial knowledge of object-oriented

Object-oriented is divided into three parts, including object-oriented analysis (OOA), Object-oriented design (OOD), Object-oriented programming (OOP).

1.1 OO Programming (Object oriented Analysis)

Typical OO programming process, should first tidy up the requirements, according to the requirements of OOA, the real-world objects abstracted into the program class or object, this time using the UML language, UML modeling, OOA output is a class or object model diagram.

The purpose of the Ood is to deal with the coupling between classes, to design the interface of a class or object, and to use a variety of design patterns (23 design patterns), such as the observer pattern, the responsibility chain pattern, and so on (then write some of the common JavaScript design patterns). Ooa and Ood are iterative processes that themselves do not have clear boundaries, but interact and constrain each other. Ooa and Ood ended up in the OOP phase and into the actual coding phase. Ooa and Ood are object-oriented programming ideas that are not related to specific languages, while OOP is an object-oriented programming tool that is relevant to the chosen language.

OOA and Ood can be reused across languages because the specific requirements are language-independent. OOP, as the bottom of the two, differs in language syntax, so oop is different.

By simply understanding that the OOP programming that we are going to learn is just the simplest part of the object-oriented code, let me summarize some of the object-oriented knowledge I have learned, not deep enough to understand, and if there are errors, please revise

Second, JavaScript object-oriented Programming-Class (1) class definition

In JavaScript we use functions to define a class

1 function Shape () {2         var x = 1; 3         var y = 24     }

This is a simple class, within the class only the Var-defined private variable, we can instantiate an object var ashape = new Shape () by the new keyword; So we instantiate an object instance, Ashape. However, the class defines a private variable, and we access it through ashape.x and ashape.y to discover that the program pops up undefined.

Here we redefine a class and test

function Shape2 () {    this. x = 1;      this. y = 2;    } var New Shape2 ();       // initializing an Instance object alert (bshape.x); alert (bshape.y);                       // popup 1 and 2 respectively

We define a public variable in the class with the name of this property and can access it successfully. In addition to defining private variables, VAR can also define private functions

function Shape3 () {           varfunction() {           // private Functions         }             Thisfunction() {          // Common functions that the outside world can see         }}    

Like the access variable, first instantiate var c = new Shape3 () , and then pass through c.draw2 (); access.

(2) Simple OOP programming

JavaScript is an interpreted language, it is not a true object-oriented language, and many object-oriented mechanisms need to be implemented by imitation.

1 //imitating OOP programming2 functionShape4 (m,n) {3         varx = 0;4         vary = 0;5 //creates an internal initialization function and executes the6         varinit =function(){7x =m;8y =N;9         }Ten init (); One //write a Get method, take out our incoming x value A          This. GetX =function(){ -             returnx; -         } the     }

The simple one mimics OOP programming, we pass var obj = new Shape4 (2,4) , instantiate an obj, and pass in 2, 42 parameters in passing obj.getx (); /c12> get our incoming x parameter, get 2.

Let's imitate the constructor of OOP programming, it is important to note that the static method in JS is on the class, not on the object.

1 function Person () {  2       this. Name = "Yanyan" 3}; 4 person.age = 0; // Static Variables 5 // Static Methods 6 function (obj) {  7          alert (obj. Name)  8}9 person.showname (new Person ());
(3) Map

There is no data for the map type (a collection of key-value pairs) in JavaScript, so we simply mimic the map type

1         functionJMap () {2                 //Private Variables3                 vararr = { };4                 //Increase5                  This. put =function(key,value) {6Arr[key] =value;7                 }8                 //Enquiry9                  This. get =function(key) {Ten                     if(Arr[key]) { One                         returnArr[key] A}Else { -                         return NULL; -                     } the                 } -                 //Delete -                  This. remove =function(key) { -                     DeleteArr[key] +                 } -                 //Traverse +                  This. Eachmap =function(FN) { A                      for(varKeyincharr) { at fn (Key,arr[key]) -                     } -                 } -             } -             varCountry =NewJMap (); -Country.put ("value1", "the"); inCountry.put ("The value2", "the"); -Country.put ("Value3", "the"); toCountry.put ("Value4", "the"); +Alert (Country.get ("04"));//reads the value of key 04 -Country.remove ("04");//Delete the value you just read theAlert (Country.get ("04"));//the popup window is now null *  $             //Traversal ReadPanax NotoginsengCountry.eachmap (function(key,value) { -document.write (key+ "--" +value+ "<br/>"); the})

by var country = new jMap () , an JMap object is instantiated, and a set of four key values is added by the Put method, at which time JMap () is used as the class, We can instantiate the data of the map type through the new method.

JavaScript Object-oriented programming (OOP) (a)-class

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.