@dmail/proto
v1.1.0
Published
Manipulate object instead of constructor with this tiny script (< 3 ko) without dependency.
Downloads
3
Readme
proto
Manipulate object instead of constructor with this tiny script (< 3 ko) without dependency.
Example
Create & extend class in plain js looks like :
function User(name){
this.name = name;
}
User.prototype.method = function(){};
var Admin = function(){
return User.apply(this, arguments);
};
Admin.prototype = Object.create(User.prototype);
Admin.prototype.constructor = Admin;
var user = new User();
var admin = new Admin();
This library lets you write it this way :
var User = proto.extend({
constructor: function(name){
this.name = name;
},
method: function(){
}
});
var Admin = User.extend();
var user = User.create();
var admin = Admin.create();
extend(...properties)
Create an object from this & define passed properties on it.
create(...arguments)
Create an object from this & call its constructor method with passed arguments.
Further reading
Browser compatibility
All browsers are supported but stuff related to custom properties depends on Object.defineProperty compat :
- Firefox 5+
- Chrome 4+
- Internet Explorer 9+
- Opera 11.6+
- Safari 5.1+
(*) In an environment without Object.defineProperty basic stuff works but the following cannot be used (Object.defineProperty, Object.getOwnPropertyDescriptor, ...)