Nirvana Playing Madonna

    @dmail/proto

    1.1.0 • Public • Published

    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, ...)

    Keywords

    none

    Install

    npm i @dmail/proto

    DownloadsWeekly Downloads

    2

    Version

    1.1.0

    License

    none

    Last publish

    Collaborators

    • dmail