Created
April 25, 2012 13:29
-
-
Save petermichaux/2489714 to your computer and use it in GitHub Desktop.
Function.prototype.new
This file contains 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
Function.prototype.new = function() { | |
var obj = Object.create(this.prototype); | |
this.apply(obj, arguments); | |
return obj; | |
}; | |
function Person(name) { | |
this.setName(name); | |
} | |
Person.prototype.getName = function() { | |
return this.name; | |
}; | |
Person.prototype.setName = function(name) { | |
this.name = name; | |
}; | |
var david = Person.new('Dave'); | |
david.getName(); // 'Dave' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A better way to implement
new
is as follows: https://gist.github.com/aaditmshah/6269739See the following StackOverflow answer: http://stackoverflow.com/a/17345713/783743