proteus

A declarative way of creating objects, properties, and classes in ES5 JavaScript

proteus

A declarative way of creating objects, properties, and classes in ES5 JavaScript

In Greek Mythology a minor sea god (son of Oceanus and Tethys) who had the power of prophecy but who would assume different shapes to avoid answering questions.

From the Greek protos "first."

Proteus is a little library of utility functions that I put together to help manage creating objects and implementing classical inheritance in JavaScript flavors 1.8+.

Interface to Object.create, however, the props argument is a plain object of properties to copy over to the newly created object. Getters and setters will be preserved.

Utility method for creating 'plain' properties on an object. Plain being {enumerable: true, writable: true, configurable: true}, the passed spec can override these defaults.

Proteus.defineProperty(obj, "propName", 42);
 
Proteus.defineProperty(
    obj,
    "methodName",
    function () { /*...*/ },
    {enumerable: false}
);

Define multiple properties.

Proteus.defineProperties(obj, [
    [obj, "propName", 42],
    [obj, "methodName", function () {
        /*...*/
    }, {
        enumerable: false
    }]
]);

Utility method for creating a getter on an object. The property definition will default to {enumerable: true, configurable: true} unless overridden with the spec object.

Utility method for creating a setter on an object. The property definition will default to {enumerable: true, configurable: true} unless overridden with the spec object.

Utility method for creating both a getter and a setter on an object. The property definition will default to {enumerable: true, configurable: true} unless overridden with the spec object.

if setter is not given, then the getter function will be used for both getting and setting

Get all property names for an object, all the way up the prototype chain.

Get a property descriptor for an object where ever it may exist in the prototype chain.

Copy properties from supplier to receiver. This method will preserve the property definitions from supplier to receiver (uses Object.getOwnPropertyDescriptor under the hood).

Copy all properties from supplier to receiver.

Copy all enumerable properties from supplier to receiver only if the property does not exist on the receiver object.

Copy all properties from supplier to receiver, but do not overwrite existing properties on receiver.

Merge enumerable properties from all objects passed as arguments onto receiver.

Merge all properties from all objects passed as arguments onto reciever.

Merge enumerable properties from all objects passed as arguments onto receiver, only if they do not exist on receiver.

Merge all properties from all objects passed as arguments onto receiver, only if they do not exist on receiver.