Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
45 lines (37 sloc) 1.11 KB
<!doctype html>
<html lang="en">
<title>JavaScript Patterns</title>
<meta charset="utf-8">
/* Title: Classical Pattern #3 - Rent and Set Prototype (a pattern that should be generally avoided)
Description: first borrow the constructor and then also set the child's prototype to point to a new instance of the constructor
// the parent constructor
function Parent(name) { = name || 'Adam';
// adding functionality to the prototype
Parent.prototype.say = function () {
// child constructor
function Child(name) {
Parent.apply(this, arguments);
Child.prototype = new Parent();
var kid = new Child("Patrick");
console.log(; // "Patrick"
console.log(typeof kid.say); // function
console.log(kid.say()); // Patrick
console.log(kid.say()); // "Adam"
// Drawback - the parent constructor is called twice, so it could be inefficient
// reference