![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
FORK::: A declarative way of creating objects, properties, and classes in ES5 JavaScript
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
.
Return a portion of the array-like object.
Return a function that is bound to call another function on the current object, or the supplied one.
Delegate a function call to another object. Additional arguments will be prepended to the function call.
Apply a method from the self
object's prototype chain.
The name
argument is optional if the function you are invoking from, and the one up the prototype chain you wish to invoke is named. e.g:
var Proteus = require("proteus"),
objA = {
someMethod: function someMethod () {
console.log("I'm object A");
}
},
objB = Proteus.create(objA, {
someMethod: function someMethod () {
Proteus.applyProto(this, arguments);
console.log("I'm object B");
}
})
;
objB.someMethod();
// => I'm object A
// => I'm object B
Call a method name
from the self
object's prototype chain passing the remaining arguments as arguments.
Note: the name
parameter is not optional.
Proteus.Class
starts off the Proteus inheritance chain (itself is a descendent of Object
). From there you can derive new classes from Proteus.Class
(or any classes that are derived from Proteus.Class
) to develop your class hierarchy.
var MyClass = Proteus.Class.derive({
// props for MyClass
}),
MySubClass = MyClass.derive({
// props for MySubClass
})
;
ProteusClass.derive(props)
As shown above, use the static methods on the core Proteus.Class
, or the Constructor functions returned by derive
, to derive new subclasses.
In addition, Proteus
allows you to define static properties on your subclass' Constructor function. Simply provide a property self
in the passed properties for your class, and those will be copied to the Constructor function instead of the Constructor's prototype.
var MyClass = Proteus.Class.derive({
self: {
// Static properties for 'MyClass'
myStaticMethod: function () {
return true;
}
},
// All other properties are put on the prototype
instancePropA: "somevalue"
});
MyClass.myStaticMethod(); // => true
(new MyClass()).instancePropA === "somevalue"; // => true
When one class is derived from another, and the superclass possesses a function property named inherited
, the function will be called and passed the newly created Constructor function (i.e: the new subclass).
var MyBaseClass = Proteus.Class.derive({
self: {
inherited: function (subclass) {
subclass.superParent = "MyBaseClass";
}
}
}),
MySubClass = MyBaseClass.derive({/* ... */})
;
MySubClass.superParent === "MyBaseClass"; // => true
Every class derived from Proteus.Class has a __super__
property that points to its super class' prototype.
var MyBaseClass = Proteus.Class.derive({
someMethod: function () {
// ...
}
}),
MySubClass = MyBaseClass.derive({
someMethod: function () {
MySubClass.__super__.someMethod.call(this);
}
})
;
ProteusClass.include(obj1, ..., objN)
You can mix-in functionality from other Constructor function prototype's, or plain objects, into Proteus derived classes with the include
method.
If passed a Constructor function, Proteus will copy the function's prototype properties to your Class' prototype. If passed a plain object, it will copy over those properties to the prototype of the class.
// Copy 'OtherClass' prototype properties to 'MyClass' prototype.
MyClass.include(OtherClass);
// Merge the passed object into 'MyClass'
MyClass.include({
// Additional properties for MyClass.prototype
});
When an object, or Constructor function, is included into a Proteus Class its included
function will be called and given the object that it was included into (the Proteus Class Constructor).
var MyModule = Proteus.Class.derive({
self: {
decorators: [],
included: function (includee) {
includee.decorators = this.decorators.slice();
}
}
}),
MyClass = Proteus.Class.derive({/* ... */})
;
MyClass.include(MyModule);
MyClass.decorators; // => []
ProteusClass.extend(obj)
You can use a ProteusClass's extend
method to extend another object with its functionality.
var MyClass = Proteus.class.derive({
self: {
someMethod: function () {
// ...
}
}
});
// elsewhere
MyClass.extend(obj);
typeof MyClass.someMethod; // => "function"
Copyright (c) 2012 Jerry Hamlet jerry@hamletink.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The Software shall be used for Good, not Evil.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
FORK::: A declarative way of creating objects, properties, and classes in ES5 JavaScript
The npm package proteus-2 receives a total of 1 weekly downloads. As such, proteus-2 popularity was classified as not popular.
We found that proteus-2 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.