JavaScript and Prototype Design Pattern
Is Javascript's prototypal inheritance borrowed from prototype design pattern?
Javascript has prototypal inheritance.
For example let’s create a constructor function:
function Person(firstname, lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
Person.prototype = {
fullname: function() {
return this.firstname + ' ' + this.lastname;
}
};
var bob = new Person("Bob", "Doe");
console.log(bob.fullname());
This is very similar to the Prototype Pattern. Whenever we want a new object, we always create it out of some prototype object.
I find that it is easier to understand the JS’s prototype inheritance when comparing it with the Prototype Pattern.
This also shows a powerful learning technique: whenever you can relate something that you don’t know with something that you know, it become much easier to grasp the new thing.
The similarity is uncanny, did JS borrowed this pattern’s idea?
Share this post
Twitter
Google+
Facebook
Reddit
LinkedIn
StumbleUpon
Email