Modify constructors to function without <code>new</code>.
create constructor functions that don't need to be called with
makes your code more safe and allows constructors to be used in more functional
contexts (such as
You can use it on a constructor while creating it:
var MyObject = newlessthissetSomethingsomething;;thissomething = value;;var instance = MyObject"Hello";instancesomething; // "Hello"
And, of course, on new ES 6/2015 style classes:
var MyObject = newlessclassconstructorsomethingthissetSomethingsomething;setSomethingvaluethissomething = value;;var instance = MyObject"Hello";instancesomething; // "Hello"
You can also use it with existing constructors from other libraries without disrupting them:
var Map = newlessgooglemapsMap;var map = MapdocumentgetElementById"map-canvas" options;// orvar User = newlessBackboneModelextend;var user = Username: "Jennifer";
Note: this description is long only because the caveat is uncommon and nuanced. In most cases, it shouldn’t affect you. :)
When an ES 2015 class is wrapped with Newless, you should be somewhat careful in trying to call it with a custom context via
Class.apply(customContext). Because of limitations imposed by the class syntax, calling a Newless ES 2015 class will always return a new object. If the custom context you provide is one that includes the Newless class anywhere in its prototype chain, the returned object will have the exact same prototype chain as the custom context, but it will be separate object.
This is generally only an issue when creating a function constructor that inherits from a class constructor. Inheriting function constructors usually works like this:
SuperConstructorcallthis;thisx = "some instance value";ObjectcreateSuperConstructorprototype;
This works just fine when
SuperConstructor is a normal constructor function or a Newless constructor function. If
SuperConstructor is a class, this doesn’t work at all. However, if
SuperConstructor is a Newless class, the are some minor oddities:
var superInstance;var SuperConstructor = newlessclassconstructorsuperInstance = this;thisy = "super constructor instance property";;var instance = ;// as you’d normally expect, all the properties are accessible:instancex === "some instance value";instancey === "super constructor instance property";// ...but technically, the `this` in the two constructors is not the same!instance !== superInstance;ObjectgetPrototypeOfinstance === superInstance;
You’ll notice above that the
this used in
SuperConstructor has become the prototype of the
this used in
SubConstructor. That’s done to make things work as smoothly as possible within the constraints created by ES 2015’s class syntax. You can avoid this slight oddity by instead coding
SubConstructor like so:
var instance = SuperConstructorcallthis;instancex = "some instance value";return instance;ObjectcreateSuperConstructorprototype;
Note that, instead of working with
this, you work with the return value of calling
SuperConstructor. If you already code inheritance this way, everything works fine. If you don’t, making this change could help you avoid some rare edge cases where you might run into trouble.
Newless is open source software. It is (c) 2013-2016 Rob Brackett and licensed under
the BSD license. The full license text is in the