JavaScript Notes – 2
Posted: October 21st, 2011 | Author: nramakrishnan | Filed under: technical | Tags: javascript | 1 Comment »In this blog post, I am introducing Custom Constructor functions, a method for “Classical Inheritance” in JavaScript and Prototypal inheritance in JavaScript. The knowledge in this post is condensed from Stoyan Stefanov’s book JavaScript Patterns.
While trying to understand Prototypal inheritance in JavaScript, I realized that I needed to grasp the above concepts strongly. So hence this post touches upon a breadth of topics.
Custom Constructor Functions
3 ways of creating objects in JS –
var myObject1 = {}; // literal notation
var myObject2 = new Object();
var myObject3 = new MyConstructor(); // custom constructor
The third way is what we are interested in currently. So, it looks the same as we would do in Java to create a new object of a class MyConstructor. Below is one way that the function MyConstructor can be defined. (Do remember that MyConstructor is a function at the end of the day. The capitalized first letter is a coding practice to differentiate custom constructors from functions)
var MyConstructor = function(name){
this.name = name;
this.hello = function () {return "Hello "+this.name;};
}
var obj = new MyConstructor("nadu");
When we call MyConstructor with new, 3 things happen inside the function
- An empty object is created and referenced by this variable, inheriting the prototype of the function.
- Properties and methods are added to the object referenced by this.
- The newly created object referenced by this is returned at the end implicitly (if no other object was returned explicitly).
In the above example, the hello was added to this. So each time new MyConstructor gets called, a new function hello is created in memory. Since the method hello does not change from one instance to another, we can add that to the prototype of MyConstructor.
MyConstructor.prototype.hello = function(){
return "Hello "+ this.name;
}
A prototype is an object and every function you create automatically gets a prototype property that points to a new blank object. You can add functions to the prototype and they will be accessible to all the objects created using new MyConstructor()
There are 2 different ways inheritance (code-reuse) can be done in JavaScript. One is to create classes to emulate the typical (class-ical) inheritance and the other is using the prototypal nature of the language.
Classical Inheritance
This can be implemented in JavaScript based on custom constructors. Below is just one pattern (simplest) of implementing classical inheritance. Refer to the book for different implementations.
// parent constructor
function Parent(name){
this.name = name || "Adam";
}
// add functionality to the prototype
Parent.prototype.say= function(){
return "Hello "+ this.name;
}
The hidden link __proto__ points to the prototype property of the constructor function that created that object.
//child constructor
function Child(){}
//inherit - C will have access to P's properties
inherit(Child,Parent);
var kid = new Child();
console.log(kid.name);//"Adam"
console.log(kid.say());
// here is how inherit is defined
function inherit(C,P){
C.prototype = new P();
}
Prototypal Inheritance
In Prototypal inheritance, objects inherit from objects. You have an object that you would like to reuse and you want to create a second object that gets functionality from the first one.
var parent = {
name:"Parent"
};
// the new object which inherits parent
var child = object(parent);
console.log(child.name); // Parent
console.log(child.prototype);
console.log(child.__proto__);
// here is how object is defined
function object(o){
function F(){};
F.prototype = o;
return new F();
}
An important thing to understand is that the child.__proto__ points to the parent object. The __proto__ link points to the prototype property of its parent constructor which was the temporary function F. The __proto__ link is what makes the interpreter check for the property name in its parent.
The console output showing the values of child.name, child.prototype, child.__proto__




Very interesting points. Thanks!
My blog:
solution rachat credit ou rachat de credits