JavaScript Object-Oriented programming

Source: Internet
Author: User

1190000002900676

Introduced

Unlike Java, a class-based (Class-base)-oriented programming language, JavaScript has no concept of class, but JavaScript is also an object-oriented language, and this object-oriented approach becomes Object-oriented based on the prototype (prototype-base). Although ES6 has introduced the concept of class as a template, through the keyword "class" can define the class, but the ES6 of this writing can be understood as a syntactic sugar, its most functions, ES5 can do, the new class writing is only to make the object prototype more clear, More like object-oriented programming syntax. If you want to understand the concept of object-oriented implementation based on prototype, then understand three important concepts in JavaScript: constructors (constructor), prototypes (prototype), prototype chains (prototype chain) It is particularly important to help understand the object-oriented thinking based on prototype. Here are some ideas to focus on.

JavaScript Object Structure diagram

Let's take a look at a schematic diagram of a JavaScript object from Mollypages.org, which is illustrated in the following example.

Constructors (constructor) and prototypes (prototype)

Constructors are used to initialize an object, and each constructor has a non-enumerable property, which is the prototype (prototype). Also, each prototype contains a constructor property that contains a non-enumerable property, which always points to the constructor.

function Foo(){}; console.log(Foo.prototype.constructor === Foo); // true

Prototype chain (prototype chain) using new instantiated prototypes

Each object that is instantiated by new will contain a __proto__ property, which is a reference to the constructor prototype .

  function Foo(){};  var foo = new Foo(); console.log(foo.__proto__ === Foo.prototype); // ture
  function Foo(){};  console.log(Foo.prototype.__proto__ === Object.prototype); // true

The reason for this is that Foo.prototype is an object that is pre-created and is an instance of object creation, so foo.prototype.__proto_ is a reference to Object.prototype.

We can look at the context of the prototype chain.

Prototype of a Function object

In JavaScript, a function is a special object, and all functions are instances of the constructor Function .

  function Foo() {};  console.log(Foo.__proto__ === Object.prototype); //false console.log(Fool.__proto__ === Function.prototype); // true

As can be seen from the above, the function foo.__proto_ points to Function.prototype, which indicates that the function Foo is an instance of functions.

function Foo(){}; console.log(Foo.__proto__ === Function.prototype); //true console.log(Foo.prototype.__proto__ === Object.prototype);//true

Foo.prototype is an object that is pre-defined, and the constructor is objects, so __proto__ point toObject.prototype

As we can see from the above figure, these functions, such as Object, function, Array, and their constructors are all instances of function.

Inheritance based on the prototype chain

With this basic knowledge, we can then encapsulate the object based on the prototype chain and implement the JavaScript inheritance. Let's take a look at the following example.

Running the above example, enter the cat init and animal eat to illustrate how cat inherits the Animal.prototype.eat method.

Let's analyze the code.

1. The Eat method is defined in the Animal prototype.
2, the Empty.prototype point to Animal.prototype, so there are methods in Empty.prototype eat .
3, Cat.prototype = = new Empty (), so cat.prototype.__proto_ = = = Animal.__proto_.
4. Re-designate Cat constructor as cat, otherwise cat does not exist constructor.

This completes the inheritance, which is the prototype chain:

In this way, we implement a simple inheritance method using the prototype chain.

JavaScript Object-Oriented programming

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.