Created
August 10, 2013 14:01
-
-
Save diegocaxito/6200557 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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