Created
December 10, 2013 15:45
-
-
Save amatiasq/7892749 to your computer and use it in GitHub Desktop.
A simple .new() method to create instances without constructors. This allow us to rewrite the "new" in order to do other things but create objects.
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 $new() { | |
var obj = Object.create(this); | |
obj.init.apply(obj, arguments); | |
return obj; | |
} |
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
var SimpleMap = { | |
new: $new, | |
init: function() { | |
this._map = Object.create(null); | |
}, | |
get: function(key) { | |
return this._map[key]; | |
}, | |
set: function(key, value) { | |
return this._map[key] = value; | |
} | |
}; | |
var map = SimpleMap.new(); |
Object.create()
set's the prototype of the new object to the value you pass to it
{}
creates a object which prototype is Object.prototype
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
porque usar Object.create(null) en vez de {} ?