A primary study of Javascript object-oriented programming (i)---encapsulation

Source: Internet
Author: User
Tags hasownproperty

Javascript is an object-based (object-based) language, and everything you encounter is almost always an object. However, it is not a true object-oriented programming (OOP) language because it has no syntax class (class).

So, what should we do if we want to encapsulate the property and method as an object and even generate an instance object from the prototype object?

first, generate the original mode of the instance object

Suppose we think of a cat as an object that has two properties of "name" and "Color".

var Cat =" "

Now we need to generate two instance objects based on the schema of the prototype object.

 var  cat1 = {}; //  Create an empty object  cat1.name =  "  woolen  ; //  assign a value according to the properties of the prototype object  Cat1.color =   yellow   ;  var  cat2 = {};cat2.name  =  "  er Mao  "   =   black  ";

Well, this is the simplest package, encapsulating two attributes in an object. However, there are two shortcomings in this writing, one is that if you generate several instances, it is very troublesome to write, and the other is that there is no way between the example and the prototype, and we can see what the connection is.

Ii. Improvement of the original model

We can write a function that solves the problem of code duplication.

function Cat (name,color) {  return  {name:name, color:color}}

Then generate the instance object, which is equivalent to calling the function:

var cat1 = Cat (" da Mao "," yellow "); var cat2 = Cat (" er mao "," black ");

The problem with this approach remains that cat1 cat2 there is no intrinsic connection between them and that they are instances of the same prototype object.

third, the structural function mode

To solve the problem of generating instances from prototype objects, JavaScript provides a constructor (Constructor) pattern.

The so-called "constructor", in fact, is a normal function, but the internal use of this variables. Using an operator on a constructor new enables you to generate an instance, and the this variable is bound to the instance object.

For example, a cat's prototype object can now be written like this,

function Cat (name,color) {  this. name=name;   this. color=color; }

We can now build the instance object.

 var  cat1 = new  Cat ( "  woolen  " ,   yellow  "   var  cat2 = new  Cat ( "  er Mao  " ,   black  "   //  da Mao  alert (cat1.color); //  yellow  

At this point cat1 cat2 , it automatically contains a constructor property that points to their constructor.

// true // true

JavaScript also provides an instanceof operator that validates the relationship between a prototype object and an instance object.

// true // true

Four, the problem of the structure function pattern

The constructor method works well, but there is a problem of wasting memory.

See, let's add a constant Cat property type (kind) to the object, and add a method eat (eat). Then the prototype object Cat becomes the following:

function Cat (name,color) {  this. Name = name;     this. color = color;     This" cat animal ";  this. Eat = function () {alert (" eat mouse ");};}

The same approach is used to generate an instance:

 var  cat1 = new  Cat ( "  woolen  " ,   yellow  "   var  cat2 = new  Cat ( "  er Mao  " ,   black  "   //  feline  cat1.eat (); //  eat mouse  

There seems to be no problem on the surface, but there is a big drawback in actually doing so. That is, for each instance object, the type properties and methods are exactly eat() the same, and each time an instance is generated, it must be a duplicate of the content and occupy more memory. This is neither environmentally friendly nor inefficient.

// false

Can the type properties and eat() methods be generated only once in memory, and then all instances point to that memory address? The answer is yes.

Five, prototype mode

JavaScript specifies that each constructor has a prototype property that points to another object. All properties and methods of this object are inherited by an instance of the constructor.

This means that we can define the invariant properties and methods directly prototype on the object.

function Cat (name,color) {  this. Name = name;  this. Color =" feline "= function () {alert ("  Eat mice ")};

Then, build the instance.

 var  cat1 = new  Cat ( "  woolen  " ,   yellow  "   var  cat2 = new  Cat ( "  er Mao  " ,   black  "   //  feline  cat1.eat (); //  eat mouse  

At this point, all instances of the type properties and methods, in fact, are the eat() same memory address, pointing prototype to the object, thus improving the efficiency of the operation.

// true
Six, the verification method of prototype mode

To match prototype the attributes, JavaScript defines some helper methods that help us to use it.

6.1 isprototypeof ()

This method is used to determine the proptotype relationship between an object and an instance.

// true // true

6.2 hasOwnProperty ()

Each instance object has a hasOwnProperty() method that is used to determine whether a property is a local property or a property inherited from an prototype object.

Alert (Cat1.hasownproperty ("name"//  true alert (Cat1.hasownproperty ( " type " // false

6.3 In operator

inThe operator can be used to determine whether an instance contains a property, whether it is a local property or not.

Alert ("name"in//  true alert ("type"  in//  true

inOperators can also be used to traverse all properties of an object.

 for (var in CAT1) {Alert ("cat1["+prop+"]="+cat1[prop]);}

Don't worry, we still have a long way to go ...

A primary study of Javascript object-oriented programming (i)---encapsulation

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.