google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






JavaScript Classical Inheritance One more JavaScript article tutorial ons jsB@nk to teach you some basic concept about JavaScript inheritance; please go to the inner post page for full details with JavaScript sample codes.

Other related JavaScript tutorials you should read:
- Simple Concepts of JavaScript Prototypal Inheritance
- 5 Chief JavaScript Inheritance Concepts


Generate your business videos by AI with voice or just text



Your first FREE AI Video App! Automate Your First AI Video. Create Your Professional Video In 5 Minutes By AI No Equipment Or Video Editing Skill Requred. Effortless Video Production For Content Marketers.
Free UNLMITED AI Video App in Your Hand

If you have written a bit of JavaScript, you certainly have heard the word prototype. Prototype is not only a JavaScript library, but also a key concept of JavaScript, which we will explore in depth today, without making your head spin.

(Almost) every object in JavaScript (where everything is an object, even functions) has an internal prototype property, which points in fact to a prototype object. When a method or property is missing on an object, it is searched on its prototype, and if not found on the prototype's prototype, and so on. the last object of the chain usually is Object.prototype.

Sometimes the prototype is exposed with the __proto__ property, although this is not part of the standard. According to the EcmaScript specification, it is a private property.

What you access with MyClass.prototype is instead a different thing. When you instance an object with new MyClass(), the object will its __proto__ set to MyClass.prototype; but MyClass.prototype is not in the prototype chain of MyClass. Its __proto__ instead is likely to be Function.prototype, since classes in JavaScript are defined as functions.

Only functions in JavaScript have this public property, which in fact can only be used for the valorization of the internal __proto__ of objects created by these functions.

A cool (but also dangerous) thing is that a reference is kept to MyClass.prototype, so you can add methods that work on *this* to MyClass.prototype later, and they will show up on every instance of MyClass, being shared by all of them.

Of course, since MyClass.prototype is an object just like everything else, its chain ultimately falls back to Object.prototype.

To implement classical inheritance, you have to leverage the prototype chains. Since properties and methods are searched on an object's __proto__, if you want it to inherit from another object you have to modify the prototype of its class (you can't modify __proto__).

Each instance of Dog now will inherit member fields from the new Animal() and consequently from Object.prototype.


This prototype chain can be technically extended as long as you want, each time by setting the prototype of the subclass equal to an object of the parent class. Of course from you should be wary of endless hierarchies.

This is the full code implementing these examples.

 

01.TestCase("playing with object prototypes in JavaScript", {
02.    "test object literals have natural prototype" : function () {
03.        var anObject = {};
04.        assertEquals(Object.prototype.toString, anObject.toString);
05.    },
06.    "test constructors have public prototype property" : function () {
07.        function MyClass() {};
08.        assertNotUndefined(MyClass.prototype);
09.    },
10.    "test objects do not have public prototype property" : function () {
11.        var anObject = {};
12.        assertUndefined(anObject.prototype);
13.    },
14.    "test objects created with a constructor has constructor property" : function () {
15.        function MyClass() {};
16.        var anObject = new MyClass();
17.        assertTrue(anObject instanceof MyClass);
18.        assertEquals(MyClass, anObject.constructor);
19.        assertEquals(MyClass.prototype, anObject.__proto__);
20.    },
21.    "test objects created with a constructor inherit from Object" : function () {
22.        function MyClass() {};
23.        var anObject = new MyClass();
24.        assertEquals(Object.prototype.toString, anObject.toString);
25.    },
26.    "test the prototype chain goes from instance to constructor" : function() {
27.        function MyClass() {};
28.        var anObject = new MyClass();
29.        MyClass.prototype.doSomething = function () {};
30.        assertEquals(MyClass.prototype.doSomething, anObject.doSomething);
31.    },
32.    "test the prototype chain goes also to Object" : function () {
33.        function MyClass() {};
34.        var anObject = new MyClass();
35.        Object.prototype.doSomethingElse = function () {};
36.        assertEquals(Object.prototype.doSomethingElse, anObject.doSomethingElse);
37.    },
38.    "test inheritance can be built by substituting prototypes with an object of the parent class" : function () {
39.        function Animal() {};
40.        Animal.prototype.eat = function() { return 'Yum'; };
41.        function Dog() {};
42.        Dog.prototype = new Animal();
43.        Dog.prototype.bark = function() { return 'Arf'; };
44.        Dog.prototype.constructor = Dog;
45.        var lassie = new Dog();
46.        assertTrue(lassie instanceof Dog);
47.        assertEquals('Arf', lassie.bark());
48.        assertTrue(lassie instanceof Animal);
49.        assertEquals('Yum', lassie.eat());
50.        assertEquals(Dog.prototype, lassie.__proto__);
51.        assertEquals(Animal.prototype, Dog.prototype.__proto__);
52.    },
53.    "test N levels of inheritance can be obtained by making each prototype an object which properties are inferred from parent prototype" : function () {
54.        function establishInheritance(childClass, parentClass) {
55.            childClass.prototype = new parentClass();
56.            childClass.prototype.constructor = childClass;
57.        };
58. 
59.        function Animal() {};
60.        Animal.prototype.eat = function() { return 'Yum'; };
61. 
62.        function Dog() {};
63.        establishInheritance(Dog, Animal);
64.        Dog.prototype.bark = function() { return 'Arf'; };
65. 
66.        function Collie() {};
67.        establishInheritance(Collie, Dog);
68. 
69.        var lassie = new Collie();
70. 
71.        assertTrue(lassie instanceof Collie);
72.        assertTrue(lassie instanceof Dog);
73.        assertTrue(lassie instanceof Animal);
74.        // inherits up to upmost class
75.        assertEquals('Yum', lassie.eat());
76. 
77.        assertEquals(Collie.prototype, lassie.__proto__);
78.        assertEquals(Dog.prototype, Collie.prototype.__proto__);
79.        assertEquals(Animal.prototype, Dog.prototype.__proto__);
80.    }
81.});
82. 
AIVideo-App.com
Generate your business videos by AI with voice or just text

chatGPTaz.com
Talk to ChatGPT by your mother language

AppAIVideo
Your first FREE AI Video App

Deepfake Video
Deepfake AI Video Maker

Deepfake
Deepfake AI Video Maker

AI Deep Fake
Deepfake AI Video Maker

AIvidio
AI Video Mobile Solutions

AIvideos
AI Video Platform & Solutions

AIvedio
AI Video App Maker

Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)

Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Powerful AI Presentation PPT Maker for FREE
Build an impressive presentation with our free online AI presentation app

Your next top AI Assistant
Claude AI, developed by Anthropic

Your next top AI Assistant
Claude AI, developed by Anthropic

Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program

Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing

Dall-E-OpenAI.com
Generate creative images automatically with AI

chatGPT4.win
Talk to ChatGPT by your mother language

First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language

Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

iPhoneKer.com
Save up to 630$ when buy new iPhone 16

Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k

JavaScript by day


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web