& Quot; Class & quot; and & quot; instance & quot;, JavaScript instance in javascript

Source: Internet
Author: User

"Class" and "instance" in JavaScript, javascript instance

JavaScript does not have a parent class, a subclass, or a class or instance. prototype chain is used to implement inheritance. when you look for an object's properties, JavaScript will traverse the prototype chain up until the corresponding properties are found. there are several methods that allow JavaScript to simulate the concepts of class and instance.

1. Use the constructor directly to create an object. Use this within the constructor to refer to the object instance.

> 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(10, 20);> point1.method1();method1> point1.method2();method2

In the preceding example, specify the prototype attribute of the constructor object. Then, a new object instance is created to call the method specified in prototype.

2. Use the Object. create () method to create an 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 easier than the constructor method, but it cannot implement private attributes and private methods, and data cannot be shared between instance objects, and the simulation of class is still not comprehensive.
3. minimalist approach proposed by the Dutch programmer Gabor de Mooij. Recommended usage.

> var Animal = {... init: function() {..... var animal = {};..... animal.name = "animal";..... animal.makeSound = function() { console.log("animal sound"); };..... return animal;..... }... };> var animal3 = Animal.init();> animal3.name;'animal'> animal3.makeSound();animal sound
Without prototype and this, you only need to customize a constructor init. Inheritance implementation 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 Reload cat sound> cat. sleep (); cat sleep
Use of private attributes and private methods: 

> Var Animal = {... init: function (){..... var animal = {};..... var sound = "private animal sound"; // private attributes ..... animal. makeSound = function () {console. log (sound );};..... return animal; ......}...};> var animal4 = Animal. init ();> animal. sound; // Private attributes can only be read through the method of the object itself. undefined> animal4.makeSound (); private animal sound
As long as the attributes and methods defined on an animal object are private, they cannot be accessed by the outside world.
Data can be shared between classes and instances.

> 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
For example, Animal. sound is a common attribute of a class and an instance. It can be regarded as a Class Attribute and a class method.

If an instance modifies this common attribute, the common attribute of this class and other instances is also modified.
In summary, it is the concept and usage of the class and instance simulated 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.