Skip to content

Instantly share code, notes, and snippets.

@diegocaxito
Created August 10, 2013 14:01
Show Gist options
  • Save diegocaxito/6200557 to your computer and use it in GitHub Desktop.
Save diegocaxito/6200557 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
</body>
</html>
/* Javascript patterns - Stoyan Stefanov - Classical Pattern #2 - Rent-a-Constructor
- This patterns solves the problem of passing arguments from the child to the parent. It borrows the parent constructor, passing the child object to be bound to 'this' and also fowarding any arguments
*/
/* Sample */
// a parent constructor
function Article(){
this.tags = ['js', 'css'];
}
var article = new Article();
// a blog post inherits from an article object via the classical pattern #1 (Inheritance)
function BlogPost(){}
BlogPost.prototype = article;
var blog = new BlogPost();
// note that above you didn't need 'new Article()' because you already had an instance available
// a static page inherits from article via the rented constructor pattern
function StaticPage(){
Article.call(this);
}
var page = new StaticPage();
console.log(article.hasOwnProperty('tags'));
console.log(blog.hasOwnProperty('tags'));
console.log(page.hasOwnProperty('tags'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment