JavaScript Design Patterns: First, object-oriented Programming (section II)

Source: Internet
Author: User

First, the package

One of the characteristics of object-oriented programming is encapsulation, and the popular way of thinking is to put the function in an object. Unfortunately, for JS this kind of explanatory weak type language does not have the classical strong type language in the same way through the class and so on keyword implementation of the package method, JS is through a number of features imitation implementation, although this is a disadvantage, but also brings a very high flexibility.

Let's see how a book class is implemented by JS:

1 //notation 12 varBook =function(ID, name, pirce) {3    This. ID =ID4    This. Name =name5    This. Price =Pirce6 }7 8 /*9 * The following two methods are not available for mixingTen */ One  A //You can also add properties and methods to the class's prototype object prototype, in two ways - //one is to assign a value of one by one to the prototype object property -Book.prototype.display =function () { the   // ... - } -  - //A prototype object that assigns an object to a class +Book.prototype = { -Displayfunction () { +     // ... A   } at}

In this way, we encapsulate the required methods and properties in our abstract book class, and when we use the function method, we cannot use the book class directly, we need to use the New keyword to instantiate the object, and to access the object's properties or methods through the point syntax. Like what:

1 var New Book (10, ' books ', '2 ')console.log (book.name)  //  Books

As you can see from the code, we've added a property through this and added a method through prototype, so what's the difference between the two?

The properties and methods added through this are added on the current object, but JS is a prototype-based prototype language, so each time a new object is created, the method inherited by prototype is not the object itself, so when using these methods, Needs to be looked up through prototype, and the property or method defined by this is owned by the object itself, so each time we create a new object, the properties and methods that this point points to are created accordingly.

Property and Method encapsulation

In OOP, since it is a class, there must be private properties, private methods, common attributes, common methods and so on these concepts, then JS in how to achieve it?

Because of the function of JS functions, the variables and methods declared inside the function are not accessible to the outside world, through the secondary attribute can create private variables and private methods of the class, but inside the function through this create the properties and methods, when the class creates objects, each object itself has a copy and can be accessed externally, So these properties and methods can be seen as common, or book as an example:

1 //private properties with private methods, privileged methods, object common properties, and object public methods2 varBook =function(ID, name, price) {3   //Private Properties4   varnum = 15   //Private Methods6   functionCheckid () {7     // ... 8   }9   //Privileged MethodsTen   //the so-called privileged method is a method that can be accessed both externally and through the private properties and methods inside the class . One    This. GetName =function () { A     // ... -   } -    This. GetPrice =function () { the     // ... -   } -    This. SetName =function () { -     // ... +   } -    This. Setprice =function () { +     // ... A   } at   //Object Public Properties -    This. ID =ID -   //Object Public Method -    This. Copy =function () { -     // ... -   } in  -}

In OOP, there are two concepts: static properties and Static methods, both of which belong to the class, and not each object, the invocation can only be used by the class name plus point syntax, then how to implement JS?

In fact, we can define properties and methods outside of the class definition through point syntax, so that when created with the New keyword, the properties and methods that are written outside of the class by point syntax are not executed, and thus can only be called by the class itself, and we achieve the purpose of imitating static properties and methods.

1 // class static Public property (object cannot be accessed) 2 true 3 // class Static Public method (object cannot be accessed) 4 function () {5   return Book.ischinese 6 }

JavaScript Design Patterns: First, object-oriented Programming (section II)

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.