Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Comparative Analysis of Common Inheritance Patterns in JavaScript

Tech 2

1. Prototype Chianing Inheritance (Simplest Approach)

function Parent(name) {
    this.name = name;
    this.favoriteSports = ['basketball', 'football'];
    this.location = { country: 'China', city: 'Jiangsu' };
}

function Child(name) {
    this.greet = function() { console.log('I am a child'); };
}

Child.prototype = new Parent(); // Core mechanism

const john = new Child('john');
console.log(john.favoriteSports); // ['basketball', 'football']

Pros:

  • Straightforward and easy to grasp
  • Minimal setup required

Cons:

  • Cannot pass arguments to the parent constructor during child instantiation
  • Shared references for objects and arrays across all instances — modifying one affects others:
const alice = new Child('alice');
alice.favoriteSports.push('tennis');
alice.location.district = 'Jianye District';

console.log(john.favoriteSports); // ['basketball', 'football', 'tennis']
console.log(john.location.district); // 'Jianye District'

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.