Last active
January 2, 2016 01:29
-
-
Save alivesay/8231102 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var BitObject = { | |
mixins: null, | |
id: 0, | |
generateID: function () { | |
return BitObject.id++; | |
}, | |
create: function () { | |
var newObject = Object.create(this); | |
newObject.id = this.generateID(); console.log(newObject.id); | |
this._construct.apply(newObject, arguments); | |
return newObject; | |
}, | |
extend: function (props, descriptors, mixins) { | |
var newObject = Object.create(this), name, propNames, i, j; | |
descriptors = descriptors || {}; | |
if (props) { | |
propNames = Object.getOwnPropertyNames(props); | |
i = propNames.length; | |
while (i--) { | |
name = propNames[i]; | |
if (!descriptors.hasOwnProperty(name)) { | |
newObject[name] = props[name]; | |
} | |
} | |
} | |
if (mixins) { | |
newObject.mixins = []; | |
i = mixins.length; | |
while (i--) { | |
newObject.mixins.push(mixins[i]); | |
if (mixins[i].mixins) { | |
j = mixins[i].mixins.length; | |
while (j--) { | |
if (BitUtil.arrayIndexOf(newObject.mixins, mixins[i].mixins[j]) === -1) { | |
newObject.mixins.push(mixins[i].mixins[j]); | |
} | |
} | |
} | |
propNames = Object.getOwnPropertyNames(mixins[i]); | |
j = propNames.length; | |
while (j--) { | |
name = propNames[j]; | |
if (!newObject.hasOwnProperty(name)) { | |
newObject[name] = mixins[i][name]; | |
} | |
} | |
} | |
} | |
if (this.mixins) { | |
i = this.mixins.length; | |
while (i--) { | |
if (BitUtil.arrayIndexOf(newObject.mixins, this.mixins[i]) === -1) { | |
newObject.mixins.push(this.mixins[i]); | |
} | |
} | |
} | |
Object.defineProperties(newObject, descriptors); | |
return newObject; | |
}, | |
_construct: bit_noop, | |
_constructMixin: function (mixin, args) { | |
if (BitUtil.arrayIndexOf(this.mixins, mixin) !== -1) { | |
mixin._construct.apply(this, args); | |
} | |
}, | |
_super: function (obj, method, args) { | |
return Object.getPrototypeOf(obj)[method].apply(this, args); | |
} | |
}; | |
var BitVector2D = BitObject.extend({ | |
x: 0, | |
y: 0, | |
_construct: function (x, y) { | |
this.x = x || this.x; | |
this.y = y || this.y; | |
}, | |
dot: function (vector) { | |
return this.x * vector.x + this.y * vector.y; | |
}, | |
... | |
}); | |
var BitVector3D = BitObject.extend({ | |
z: 0, | |
_construct: function (x, y, z) { | |
this.z = z || this.z; | |
this._consructMixin(BitVector2D, [x,y]); | |
}, | |
dot: function (vector) { | |
return this.x * vector.x + this.y * vector.y + this.z * vector.z; | |
}, | |
... | |
}, null, [BitVector2D]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment