Created
May 23, 2011 09:49
-
-
Save Gozala/986487 to your computer and use it in GitHub Desktop.
Alternative sugar for classes in ES.next
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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