Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Created December 10, 2013 15:45
Show Gist options
  • Save amatiasq/7892749 to your computer and use it in GitHub Desktop.
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.
function $new() {
var obj = Object.create(this);
obj.init.apply(obj, arguments);
return obj;
}
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();
@amatiasq
Copy link
Author

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