This function calls a super method dynamically.
While it is slower than the normal way to do supercalls with
dcl (see the decorator
dcl.superCall() for details), it doesn’t require to modify
a method according to the double function pattern, and can be applied to
undecorated methods, which makes it suitable for fast prototyping, and
transitioning legacy code.
As soon as
inherited.js is included, it mixes in
inherited() as a method to all
newly created objects, so you don’t need to use the result of the module directly.
inherited() can be used in strict mode.
Unlike the decorator dcl.superCall(), which supplies
a supercall method statically,
inherited() does the same dynamically at some
run-time expense. While doing so, it doesn’t require to decorate a method, and
can be used to add a supercall to any arbitrary method, which makes it suitable
for transitioning legacy code or fast prototyping:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
As you can see
calcPrice1() is a simple undecorated method,
the fastest way to do supercalls, and
calcPrice3() is similar to
yet employs a simple optimization technique: determines a supercall once instead
of three times and reuses its result.
The example above demonstrates three possible ways to use
this.inherited(arguments)is probably the most common case: it calls a super method with the same arguments as the current methods. Effectively it is a pass-through. This style of code cannot be used in strict mode.
argumentsonly to figure out a super method, which will be called with following array of arguments:
[x]. It is a way to call a super with a custom list of arguments. This style of code cannot be used in strict mode.
this.inherited(B, "calcPrice3", [x])is a direct way to identify what super method should be used. In this example
Bis a constructor object for a class with our method,
"calcPrice3"is a name of that method, and
[x]is an arbitrary array of parameters we want to pass. This style of code works perfectly in strict mode.
- The downside of this method is a necessity to specify method’s name explicitly. Usually it is known statically, so it is not a problem, yet it may make method renaming error-prone.
- Remember: a constructor and a name specify a method that calls its super, not its super method. It cannot name a method different from a current method!
inherited() returns a result returned by a super method, whatever it may be.
If there is no super method, the call is essentially a no-op and returns
If you want to check for an existance of a super method,
- If a method is present in an
Object, it will be the last in line of potential super calls.
- If a super method throws an exception, it is a programmer’s responsibility to catch it, to ignore it, or to pass it through.
Can I call built-in functions with
1 2 3 4 5 6 7 8 9 10 11 12 13
Can I use
inherited() in a chained method?
No. It is meant mostly for legacy scenarios. While it doesn’t fail,
will not break a chain, and your chained method can be called twice.
How can I organize pass-through in strict mode?
If you want to call a super method with the same array of arguments as you received,
arguments object as your array of arguments:
1 2 3 4 5 6 7 8 9 10 11 12 13