![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.
JavaScript object inheritance sugar: Easy extension, mixins, super methods, proxies
Uberproto is a simple base object that adds some sugar to ECMAScript 5 style object inheritance in JavaScript.
Here is what it can do in a nutshell:
With a small footprint (0.8Kb minified and 0.4Kb compressed) and an easy to handle API of just four methods it also doesn't add a lot of baggage to your JavaScript application.
UberProto can be used as a CommonJS AMD module (e.g. with RequireJS), NodeJS or directly in the browser. If no module loader is available, the global variable Proto will be defined after you include the script.
Make sure proto.js is in the right folder and then just define a module like this:
define(['proto'], function(Proto) {
// Source goes here
});
<script type="text/javascript" src="proto.min.js"></script>
Now Proto is available as a global vairable.
After installing the package using NPM
npm install uberproto
just require it like any other module:
var Proto = require('uberproto');
You can extend any UberProto object by using extend to create a new object that inherits from the current one. Internally Object.create is being used (the library provides a polyfill for browsers that don't support Object.create) and the prototype is set to the object that you are extending. If defined, the init method will be used as the constructor. That way you can define a simple Person object (which will be reused throughout the next paragraphs):
var Person = Proto.extend({
init : function(name) {
this.name = name;
},
fullName : function() {
return this.name;
}
});
You can also define a plain object and pass it to UberProto object methods:
var PersonObject = {
init : function(name) {
this.name = name;
},
fullName : function() {
return this.name;
}
};
You can create a new instance by calling create:
var dave = Person.create('Dave');
console.log(dave.name); // -> 'Dave'
console.log(dave.fullName()); // -> 'Dave'
For calling the constructor on a plain object, call create on an UberProto object:
var john = Proto.create.call(PersonObject, 'John');
console.log(john.fullName()); // -> 'John'
Overwriting create is great if you want to customize the way new objects are being instantiated.
In each method this.\_super
refers to the method being overwritten, if there is one.
For our Person object, for example, it would be a lot better if it also had a last name:
var BetterPerson = Person.extend({
init : function(name, lastname) {
// If you want to pass all original arguments to the
// _super method just use apply:
// this._super.apply(this, arguments);
this._super(name);
this.lastname = lastname;
},
fullName : function() {
return this._super() + ' ' + this.lastname;
}
});
var dave = BetterPerson.create('Dave', 'Doe');
console.log(dave.name); // -> 'Dave'
console.log(dave.lastname); // -> 'Doe'
console.log(dave.fullName()); // -> 'Dave Doe'
You can also extend a plain object if you don't want to inherit from an UberProto object:
var BetterPersonObject = Proto.extend({
init : function(name, lastname) {
this._super(name);
this.lastname = lastname;
},
fullName : function() {
return this._super() + ' ' + this.lastname;
}
}, PersonObject); // Pass the plain object as the second parameter
Mixins add functionality to an existing object. Mixins can also access their super methods using this.\_super
.
This will either refer the overwritten method on the object itself or the one on the prototype:
Person.mixin({
init : function()
{
this._super.apply(this, arguments);
this.can_sing = true;
},
sing : function()
{
return 'Laaaa';
}
});
var dude = Person.create('Dude');
console.log(dude.sing()); // -> 'Laaaa'
console.log(dude.can_sing); // -> true
Actual instances can be mixed in just the same:
var operaSinger = Person.create('Pavarotti');
operaSinger.mixin({
sing : function()
{
return this._super() + ' Laalaaa!';
}
});
console.log(operaSinger.sing()); // -> 'Laaaa Laalaaa!
And you can also mix into plain objects e.g. overwriting the constructor of PersonObject:
Proto.mixin({
fullName : function() {
return 'My name is: ' + this._super();
}
}, PersonObject);
// Create a plain object without calling the constructor
var instance = Object.create(PersonObject);
instance.name = 'Dude';
console.log(instance.fullName()); // 'My name is: Dude'
You can create proxy callbacks, that make sure that this will always point to the right object:
var callback = operaSinger.proxy('fullName');
console.log(callback()); // -> 'Pavarotti'
And of course proxy methods of plain objects:
var cb = Proto.proxy('fullName', PersonObject);
1.0.1 (2012-03-24)
FAQs
JavaScript object inheritance sugar: Easy extension, mixins, super methods, proxies
The npm package uberproto receives a total of 23,185 weekly downloads. As such, uberproto popularity was classified as popular.
We found that uberproto 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.