I'm not sure about es5, but it is possible to simulate es6 classes without using the class
syntax, using other es6 features instead.
For example, your example
class MyFunc extends Function{}
new MyFunc('alert("hi guys")')()
Can be simulated using old style classes and Reflect.construct
as follows
function MyFunc(...args) {return Reflect.construct(Function, args, new.target);}
new MyFunc('alert("hi guys")')()
Reflect.construct
does what the super()
call in a real subclass constructor would do, which is all you need for the example posted. If you want to properly inherit all the properties and static methods, you need to additionally set the prototypes like this
function MyFunc(...args) {return Reflect.construct(Function, args, new.target);}
Object.setPrototypeOf(MyFunc, Function);
Object.setPrototypeOf(MyFunc.prototype, Function.prototype);
new MyFunc('alert("hi guys")')()
This also works for the array example
function MyArr(...args) {return Reflect.construct(Array, args, new.target);}
Object.setPrototypeOf(MyArr, Array);
Object.setPrototypeOf(MyArr.prototype, Array.prototype);
var myarr = new MyArr(1,2,3,4)
myarr[2] = "a value"
myarr.push("an other value")
The first set prototype call is only required to inherit static properties like Array.from
. If you don't care about those, you can get away with only setting up the prototype of the prototype object, in which case you don't need Object.setPrototypeOf
at all. You can instead use Object.create
as follows:
function MyArr(...args) {return Reflect.construct(Array, args, new.target);}
MyArr.prototype = Object.create(Array.prototype);
MyArr.prototype.constructor = MyArr;
var myarr = new MyArr(1,2,3,4)
myarr[2] = "a value"
myarr.push("an other value")
For more details, see this post.