"Classes" and "instances" in JavaScript

Source: Internet
Author: User

There is no parent, subclass, or class or instance concept in JavaScript, and it relies on prototype chain to implement inheritance. When you find the properties of an object, JavaScript traverses the prototype chain up until the corresponding property is found. There are several ways to make JavaScript simulate the concept of class and instance.

1, use the constructor directly to create the object, and use this to refer to the object instance inside the constructor.

> Function Animal () {... this.name = "Animal"; ...} > Animal.prototype.makeSound = function () {... console.log ("Animal sound"); [function]> var animal1 = new Animal ();> animal1.name; ' Animal ' > Animal1.makesound (); Animal sound
Let's look at another example:

> Function point (x, y) {... this.x = x; .... this.y = y; ...} > Point.prototype = {... method1:function () {Console.log ("Method1"),},... method2:function () {Console.log ("method2 "); },... } {method1: [function], METHOD2: [function]}> var point1 = new Point (Ten);> point1.method1 ();method1> point1.m ETHOD2 (); method2

Above, first specify the prototype property of the good one constructor object. Then new to the object instance, you can call the method specified in prototype.

2, use the Object.create () method to create the object

> var Animal = {... name: "Animal",... makesound:function () {console.log ("Animal Sound");},...} > var animal2 = object.create (Animal);> animal2.name; ' Animal ' > Console.log (animal2.name);animal> animal2.makesound (); Animal sound
This method is simpler than the constructor method, but it cannot implement private properties and private methods, and the data cannot be shared between instance objects, and the simulation of class is still not comprehensive enough.
3, Dutch programmer Gabor de Mooij proposed a minimalist approach (minimalist approach). Recommended usage.

> var Animal = {... init:function () {..... var Animal = {}; ... animal.name = "Animal"; animal.makesound = funct Ion () {Console.log ("animal Sound");}; ...... return animal; ... }; > var animal3 = animal.init ();> animal3.name; ' Animal ' > Animal3.makesound (); Animal sound
Without using prototype and this, you only need to customize a constructor init. The implementation of inheritance is also very simple.
> var cat = {... init:function ()} {.... var cat = Animal.init (); cat.name2 = "Cat" ... cat.makesound = function () {Console.log ("cat Sound");}; ... cat.sleep = function () {Console.log ("cat Sleep");}; ..... return cat; ...} ... } > var cat = cat.init ();> cat.name; ' Animal ' > cat.name2; ' Cat ' > Cat.makesound (); Method-like heavy-duty cat sound> cat.sleep (); Cat sleep
Private properties and the use of private methods:

> var Animal = {... init:function () {.... var Animal = {}; var = "Private Animal sound";//private Properties ..... anim Al.makesound = function () {console.log (sound);}; ...... return animal; ... }; > var animal4 = animal.init ();> animal.sound; Private properties can only be read by the object's own method .undefined> Animal4.makesound ();p rivate animal Sound
As long as the properties and methods that are not defined on the animal object are private, the outside world cannot access them.
Between classes and instances, data sharing can be done.

> var Animal = {... sound: ' Common Animal Sound ',... init:function () {.... var Animal = {}; ... animal.commonsound = function () {console.log (animal.sound);}; ... animal.changesound = function () {animal.sound = "common animal sound Changed";}; ...... return animal; ... } > var animal5 = animal.init ();> var animal6 = animal.init ();> animal.sound; ' Common animal Sound ' > animal5.sound;undefined> animal6.sound;undefined> animal5.commonsound (); Common animal Sound> animal6.commonsound (); Common animal sound> animal5.changesound ();undefined> animal.sound; ' Common animal Sound ' > Animal5.commonsound (), Common animal sound> animal6.commonsound (); Common animal Sound
such as Animal.sound is a common property of classes and instances, and can be considered class properties and class methods.

If an instance modifies the common attribute, the common attributes for that class and other instances are also modified.
In summary, it is the concept and usage of class and instance that are modeled in JavaScript.



"Classes" and "instances" in JavaScript

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.