JS Object-oriented

Source: Internet
Author: User

1. Encapsulation

In JavaScript, encapsulation only manifests public, private

①public public, in the constructor is declared through the This keyword members are public, function inside and outside can be called;

②private Private, declare a local variable inside a constructor, which is a private member.

//Package CharacteristicsfunctionDog () {//Private Member (local variable)    varweight=120;//Private member Properties    varRun =function(){//Private Member Methods        return"5 miles a Day"; }    //Private members allow access inside the constructor    //indirect access to private members can be made in a public way     This. fight=function() {Console.log (' My weight is: ' +weight+ ', and I also ' +run () + '); }     This. mingzi= "Wang Choi";  This. hobby= "Reading";  This. leg=4;}varblack=NewDog (); Console.log (Black.fight ());//my weight is: 120, and I also run 5 kilometers a day
2. Inheritance

Inside JavaScript: the (construct) function inherits another object, and the constructor instantiates an object that has members of the inherited object in addition to its own members.

Prototype Inheritance keyword: prototype.

varcat={mignzi:' Kitty ', climb:function() {Console.log ( This. mingzi+ "will climb the mountain! "); }}functionTiger () { This. color= ' Yellow ';  This. leg=4;  This. act= "Rampage";}//constructor to inherit objectsTiger.prototype=Cat;varNorth=NewTiger (); Console.log (north);

2.1 Implementing prototype Inheritance

1. function. prototype. member name = value; Inherit a single member

2. function. prototype= object; Inherit an Object

functionTiger () { This. color= ' Yellow ';  This. leg=4;  This. act= "Rampage";}//prototype inheritance: Inheriting a single member//you can inherit multipletiger.prototype.weight=230; Tiger.prototype.run=function() {Console.log ("Run in the forest!" ");}varNorth=NewTiger (); Console.log (north);//Object member: its Own + inheritedNorth.run ();

Attention:

The ① itself and the inheriting object have members of the same name, to use their own members;

② single Member and object inherit at the same time, inherit a single member after inheriting the object first;

③ inherits multiple objects at the same time, which overrides the former.

//the ① itself and the inheriting object have members of the same name, to use their own members;varPig={weight:120,hobby: ' Sleep '};functionDog () { This. name= "Wang Choi";  This. color= "Gray";  This. hobby= "Housekeeping";} Dog.prototype=pig;//Inheritancevarblack=NewDog (); Console.log (black.hobby);//housekeeping, not sleep .//② single Member and object inherit at the same time, inherit a single member after inheriting the object first;varPig={weight:120,hobby: ' Sleep '};functionDog () { This. name= "Wang Choi";  This. color= "Gray";  This. hobby= "Housekeeping";} Dog.prototype=pig;//inheriting objects ///Location 1Dog.prototype.run= "in Running";//inherit a single member//Location 2varblack=NewDog (); Console.log (black);//If you inherit a single member after inheriting the object first, the object member: the + of the inherited object + the single member of the//If you inherit a single member after inheriting the object, the object member: its own + inherits the object's. That is, position 2 is not a single member of run before position 1//③ inherits multiple objects at the same time, which overrides the former. varPig={weight:120,hobby: ' Sleep '};varCAT={CLIMB:function() {Console.log ("Climbing the Tree"));}};varTiger={act: "Rampage"};functionDog () { This. name= "Wang Choi";  This. color= "Gray";  This. hobby= "Housekeeping";} Dog.prototype=pig;//Inheriting ObjectsDog.prototype=cat;//Inheriting ObjectsDog.prototype=tiger;//Inheriting Objectsvarblack=NewDog (); Console.log (black);//Object member: The + of itself inherits the Tiger object (the latter overrides the former)
2.2 Object Traversal
varPig={weight:120,hobby: ' Sleep '};functionDog () { This. name= "Wang Choi";  This. color= "Gray";  This. hobby= "Housekeeping";} Dog.prototype=pig;//Inheriting Objectsvarblack=NewDog ();//For member variable in object {} Traversal member for(varKinchBlack) {    //differentiate itself from members and inherited members    //object. hasOwnProperty (member name) determines whether the member is owned by itself, returns a Boolean value    if(Black.hasownproperty (k)) {Console.log ("Own property" +k+ "=====" +Black[k]); }    Else{Console.log ("Inherited attribute" +k+ "=====" +Black[k]); }}
2.3 Copy Inheritance – prototype derivation inheritance
//Copy Inheritance//This inheritance has a large number of applications in the Juqery framework//the inheritance features/benefits: Inheritance is very flexible, can be based on actual needs//To inherit a member for an object, if the object does not require additional members, it//does not inherit, and the inheritance allows multiple objects to be inherited at the same timevarCat={color: ' White ', Weapon: ' GLib ', climb:function() {Console.log (' climbing the tree '));}};functionDog () { This. color= "Gray";  This. leg=4;} Dog.prototype.extend=function(obj) {//this represents the current object that called the method    //iterates over obj, gets a specific member, and gives the black (this) object     for(varKinchobj) {        //inheriting a member that does not exist by itself        if(! This. hasOwnProperty (k)) {             This[K]=obj[k];//enrich members directly for this object        }    }}varblack=NewDog ();//allow black objects to inherit cat objectsBlack.extend (cat); Console.log (black);
2.4 Static Members

If there is such a property within a class, its value is not arbitrarily changed, all objects share a value, and the property is declared static, and it occupies only one space in memory regardless of how many objects are created (or not related to its specific object).

Why static members are used:

① static members to the class itself can significantly speed up access (no instantiation of objects)

② saves Space

A class, each of which instantiates an object, allocates memory space to each member in the heap space, and if there is a member whose information value does not change regardless of how many objects are created, the member is set to "static member" in order to save memory space. It occupies only one space in memory.

If you think the article is good, please Bo Master drink a cup of tea Oh!!!

JS Object-oriented

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.