Skip to content

Instantly share code, notes, and snippets.

@Gozala
Created May 23, 2011 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gozala/986487 to your computer and use it in GitHub Desktop.
Save Gozala/986487 to your computer and use it in GitHub Desktop.
Alternative sugar for classes in ES.next
// define a new type SkinnedMesh and a constructor for it
function SkinnedMesh(geometry, materials) {
// call the superclass constructor
THREE.Mesh.call(this, geometry, materials);
// initialize instance fields
this.identityMatrix = new THREE.Matrix4();
this.bones = [];
this.boneMatrices = [];
...
};
// inherit behavior from Mesh
SkinnedMesh.prototype = Object.create(THREE.Mesh.prototype);
SkinnedMesh.prototype.constructor = SkinnedMesh;
// define an overridden update() method
SkinnedMesh.prototype.update = function(camera) {
...
// call base version of same method
THREE.Mesh.prototype.update.call(this);
};
Function.prototype.extend = function extend(prototype) {
// Ideally it will be
// `var constructor = this <: prototype.constructor`
// But we still can implement this today:
var base = prototype.constructor;
var source = base.toString();
// We use `eval` just to preserve `.length` and `.name` from the original `.constructor`
var constructor = eval('(' + source.substr(0, source.indexOf('{')) +
'{ return base.apply(this, arguments); })');
// Copy all the properties except prototype.
Object.getOwnPropertyNames(base).forEach(function(name) {
if (name === 'prototype') return;
Object.defineProperty(constructor, name, Object.getOwnPropertyDescriptor(base, name));
});
// Inherit all the static properties.
constructor.__proto__ = this;
// Ideally it will be:
// constructor.prototype = this.prototype <: prototype;
// But still can be implemented today:
constructor.prototype = Object.create(this.prototype, {
constructor: { value: constructor }
});
// Copy all properties except constructor.
Object.getOwnPropertyNames(prototype).forEach(function(name) {
if (name === 'constructor') return;
Object.defineProperty(constructor.prototype, name,
Object.getOwnPropertyDescriptor(prototype, name));
});
return constructor;
}
function super() {
// constructor that called method (super.caller is a function that actual
// constructor delegates to so we need to get it's caller to get to an actual
// constructor. Also, we use deprecated `caller` assuming that engine itself
// can easily get access it.
var constructor = Super.caller.caller;
return Object.getPrototypeOf(constructor.prototype).constructor.apply(this, arguments)
}
const SkinnedMesh = THREE.Mesh.extend({
constructor: function SkinnedMesh(geometry, materials) {
super(geometry, materials);
// Desugars to:
// Object.getPrototypeOf(Object.getPrototypeOf(this)).constructor.call(this, geometry, materials);
this.identityMatrix = new THREE.Matrix4;
this.bones = [];
this.boneMatrices = [];
},
// define an overridden update() method
update: function(camera) {
....
super.update();
// Desugars to:
// Object.getPrototypeOf(Object.getPrototypeOf(this)).update.call(this);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment