Skip to content

Instantly share code, notes, and snippets.

@Gozala Gozala/example.js Secret
Created Jun 15, 2011

Embed
What would you like to do?
Pure prototypial inheritance in JS.
const SkinnedMesh = THREE.Mesh.extend({
new: function SkinnedMesh(geometry, materials) {
var self = THREE.Mesh.new.apply(SkinnedMesh, arguments);
// initialize instance properties
self.identityMatrix = THREE.Matrix4.new();
self.bones = [];
self.boneMatrices = [];
return self;
},
update: function(camera) {
...
// call base version of same method
THREE.Mesh.update.call(this);
}
});
/* vim:set ts=2 sw=2 sts=2 expandtab */
/*jshint undef: true es5: true node: true devel: true evil: true
forin: true latedef: false supernew: true */
/*global define: true */
"use strict";
Object.prototype.new = function new() {
return new this.constructor();
};
Object.prototype.extend = function extend(properties) {
var constructor = new Function(), descriptor = {};
properties = properties || {};
Object.getOwnPropertyNames(properties).forEach(function(name) {
descriptor[name] = Object.getOwnPropertyDescriptor(properties, name);
});
descriptor.constructor = { value: constructor };
return Object.freeze(constructor.prototype = Object.create(this, descriptor));
};
Object.new = function new() {
return Object.prototype.new();
};
Object.extend = function extend(properties) {
return Object.prototype.extend(properties);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.