Jemt / Fit.UI

Fit.UI is a JavaScript based UI framework built on Object Oriented principles
http://fitui.org
GNU Lesser General Public License v3.0
19 stars 7 forks source link

Make sure objects are created with 'new' #65

Open Jemt opened 5 years ago

Jemt commented 5 years ago

Make sure objects are created with the 'new' keyword.

// WRONG!
// Won't fail but no instance is returned.
// input1 remains 'undefined'.
var input1 = Fit.Controls.Input();

// Correct
var input2 = new Fit.Controls.Input();

Use the following line of code in every object to make sure it is initialized properly:

if (this instanceof Fit.Controls.Input) {
    throw "Fit.Controls.Input must be initialized with the 'new' keyword";
}

Do not perform this check on e.g. Fit.Controls.ControlBase using Fit.Core.InstanceOf(this, Fit.Controls.ControlBase) since it checks the inheritance tree information created with Fit.Core.Extend(..). But unfortunately Fit.Core.Extend(..) is not capable of determining whether a given object was properly initialized (with 'new'), so it will apply the type information no matter what, which in turn causes Fit.Core.InstanceOf(..) to return True.