JavaScript USES Prototype to implement an object oriented approach

  • 2020-05-27 04:26:14
  • OfStack

This article illustrates an object-oriented approach to JavaScript using Prototype as an example. Share with you for your reference. The specific analysis is as follows:

prototype is a property of an Function object that points to another object. All the properties and methods of this object are inherited by the constructor's instance.

At the same time, prototype also has a reference to the constructor, constructor, which successfully forms a prototype chain structure of a circular reference.

We can save memory overhead by defining those immutable properties and methods directly on prototype objects.

function Cat(name, color) { = name;
  this.color = color;
Cat.prototype.type = 'mammal'; = function() {
  console.log('eat fish');
var cat1 = new Cat('Kitty', 'white');
var cat2 = new Cat('Smokey', 'black');
console.log(cat1.type); // mammal
console.log(cat1.eta === cat2.eta);
// TRUE, same reference
console.log(cat1.constructor === Cat)
// TRUE, from Person.prototype

I hope this article is helpful to you in javascript programming.

Related articles: