diff --git a/src/constraints/AngleConstraint.js b/src/constraints/AngleConstraint.js index 37e9fbd..c54f46f 100644 --- a/src/constraints/AngleConstraint.js +++ b/src/constraints/AngleConstraint.js @@ -52,9 +52,7 @@ function AngleConstraint(angle, a, b, c) { @method create @static */ -AngleConstraint.create = lib.ctor(AngleConstraint); -AngleConstraint.prototype = Object.create(lib.Constraint.prototype); -AngleConstraint.prototype.constructor = AngleConstraint; +lib.inherit(AngleConstraint, lib.Constraint); /** Set angle diff --git a/src/constraints/AxisConstraint.js b/src/constraints/AxisConstraint.js index db90541..c19694f 100644 --- a/src/constraints/AxisConstraint.js +++ b/src/constraints/AxisConstraint.js @@ -43,9 +43,7 @@ function AxisConstraint(axisA, axisB, a) { @method create @static */ -AxisConstraint.create = lib.ctor(AxisConstraint); -AxisConstraint.prototype = Object.create(lib.Constraint.prototype); -AxisConstraint.prototype.constructor = AxisConstraint; +lib.inherit(AxisConstraint, lib.Constraint); /** Set particles defining constraint axis diff --git a/src/constraints/BoundingPlaneConstraint.js b/src/constraints/BoundingPlaneConstraint.js index 06239c5..fc8c5b9 100644 --- a/src/constraints/BoundingPlaneConstraint.js +++ b/src/constraints/BoundingPlaneConstraint.js @@ -69,9 +69,7 @@ function BoundingPlaneConstraint(origin, normal, distance) { @method create @static */ -BoundingPlaneConstraint.create = lib.ctor(BoundingPlaneConstraint); -BoundingPlaneConstraint.prototype = Object.create(lib.Constraint.prototype); -BoundingPlaneConstraint.prototype.constructor = BoundingPlaneConstraint; +lib.inherit(BoundingPlaneConstraint, lib.Constraint); /** Global constraint flag diff --git a/src/constraints/BoxConstraint.js b/src/constraints/BoxConstraint.js index ca6f4e3..f8db0c0 100644 --- a/src/constraints/BoxConstraint.js +++ b/src/constraints/BoxConstraint.js @@ -54,9 +54,7 @@ function BoxConstraint(min, max) { @method create @static */ -BoxConstraint.create = lib.ctor(BoxConstraint); -BoxConstraint.prototype = Object.create(lib.Constraint.prototype); -BoxConstraint.prototype.constructor = BoxConstraint; +lib.inherit(BoxConstraint, lib.Constraint); /** Global constraint flag diff --git a/src/constraints/Constraint.js b/src/constraints/Constraint.js index 312b8c9..d1bc66d 100644 --- a/src/constraints/Constraint.js +++ b/src/constraints/Constraint.js @@ -66,7 +66,7 @@ function Constraint(size, itemSize, indexOffset) { @method create @static */ -Constraint.create = lib.ctor(Constraint); +lib.inherit(Constraint, Object); /** Set particle indices with `Array` or list of `arguments`. diff --git a/src/constraints/DistanceConstraint.js b/src/constraints/DistanceConstraint.js index 49cbc74..eeccb86 100644 --- a/src/constraints/DistanceConstraint.js +++ b/src/constraints/DistanceConstraint.js @@ -50,9 +50,7 @@ function DistanceConstraint(distance, a, b) { @method create @static */ -DistanceConstraint.create = lib.ctor(DistanceConstraint); -DistanceConstraint.prototype = Object.create(lib.Constraint.prototype); -DistanceConstraint.prototype.constructor = DistanceConstraint; +lib.inherit(DistanceConstraint, lib.Constraint); /** Set distance diff --git a/src/constraints/PlaneConstraint.js b/src/constraints/PlaneConstraint.js index 47aa22b..7093c67 100644 --- a/src/constraints/PlaneConstraint.js +++ b/src/constraints/PlaneConstraint.js @@ -54,9 +54,7 @@ function PlaneConstraint(planeA, planeB, planeC, a) { @method create @static */ -PlaneConstraint.create = lib.ctor(PlaneConstraint); -PlaneConstraint.prototype = Object.create(lib.Constraint.prototype); -PlaneConstraint.prototype.constructor = PlaneConstraint; +lib.inherit(PlaneConstraint, lib.Constraint); /** Set particles defining constraint plane diff --git a/src/constraints/PointConstraint.js b/src/constraints/PointConstraint.js index 0c4dd28..2864b94 100644 --- a/src/constraints/PointConstraint.js +++ b/src/constraints/PointConstraint.js @@ -50,9 +50,7 @@ function PointConstraint(position, a) { @method create @static */ -PointConstraint.create = lib.ctor(PointConstraint); -PointConstraint.prototype = Object.create(lib.Constraint.prototype); -PointConstraint.prototype.constructor = PointConstraint; +lib.inherit(PointConstraint, lib.Constraint); /** Set point position. diff --git a/src/forces/DirectionalForce.js b/src/forces/DirectionalForce.js index 0cc267b..1cb851c 100644 --- a/src/forces/DirectionalForce.js +++ b/src/forces/DirectionalForce.js @@ -32,9 +32,7 @@ function DirectionalForce(vector) { @method create @static */ -DirectionalForce.create = lib.ctor(DirectionalForce); -DirectionalForce.prototype = Object.create(lib.Force.prototype); -DirectionalForce.prototype.constructor = DirectionalForce; +lib.inherit(DirectionalForce, lib.Force); DirectionalForce.prototype.applyForce = function (ix, f0, p0, p1) { var v0 = this.vector; diff --git a/src/forces/Force.js b/src/forces/Force.js index f602b8d..0bab8a4 100644 --- a/src/forces/Force.js +++ b/src/forces/Force.js @@ -35,7 +35,7 @@ function Force(vector, opts) { @method create @static */ -Force.create = lib.ctor(Force); +lib.inherit(Force, Object); /** Force type enum: `Force.ATTRACTOR`, `Force.REPULSOR`, `Force.ATTRACTOR_REPULSOR`. diff --git a/src/forces/PointForce.js b/src/forces/PointForce.js index f9c8355..84d43e4 100644 --- a/src/forces/PointForce.js +++ b/src/forces/PointForce.js @@ -56,9 +56,7 @@ var pf_ATTRACTOR_REPULSOR = lib.Force.ATTRACTOR_REPULSOR; @method create @static */ -PointForce.create = lib.ctor(PointForce); -PointForce.prototype = Object.create(lib.Force.prototype); -PointForce.prototype.constructor = PointForce; +lib.inherit(PointForce, lib.Force); /** Set radius diff --git a/src/systems/ParticleSystem.js b/src/systems/ParticleSystem.js index f450f2e..99dfe15 100644 --- a/src/systems/ParticleSystem.js +++ b/src/systems/ParticleSystem.js @@ -85,8 +85,7 @@ function ParticleSystem(particles, iterations) { @method create @static */ -ParticleSystem.create = lib.ctor(ParticleSystem); -ParticleSystem.prototype.constructor = ParticleSystem; +lib.inherit(ParticleSystem, Object); /** Alias for `Vec3.set`. Sets vector of `positions` and `positionsPrev`. diff --git a/src/utils/Creator.js b/src/utils/Creator.js index 6ac967f..0732a7c 100644 --- a/src/utils/Creator.js +++ b/src/utils/Creator.js @@ -19,3 +19,18 @@ lib.ctor = function ctor(Ctor) { return instance; }; }; + +/** + Functional inheritance utility + + @method inherit + @param {Function} Ctor Class constructor + @param {Function} ParentCtor Parent class constructor + @private + @static +*/ +lib.inherit = function inherit(Ctor, ParentCtor) { + Ctor.create = lib.ctor(Ctor); + Ctor.prototype = Object.create(ParentCtor.prototype); + Ctor.prototype.constructor = Ctor; +};