Oneself turns methods into functions with an explicit self.
So instead of writing:
Arrayprototypeslice;
you can create a reusable function that takes this
as the first parameter.
var slice = ;...
As a convenenience, oneself
provides all Array
methods
as functions under oneself.array
, along with functions
in string
, date
, regexp
and object
.
var slice = oneselfarrayslice;... var map = oneselfarraymap getFullYear = oneselfdategetFullYear;;
If desired, you can extend the object itself:
oneselfarray;oneselfstring; ;
As shown above, these functions are useful to map this
over
collections of objects. oneself
also provides placeholder functions
that allow you to map methods with specified arguments, named
the same as the methods, with a _
suffix.
;
Objects
You can create functions from any object as an alternative to binding
this
.
{ thisx = x; thisy = y; } { return x y; } Pointprototype{ return '('+thisx+', '+thisy+')'; }; Pointprototype{ return 'Point '+this; }; Pointprototype{ return thisx + x thisy+y; }; var p1 = ; var describePoint = ; ;
As well as placeholder functions:
var add1_1 = ;;
Although it might be better to shim your objects. This
will create functions for all enumerable methods
along with toString
.
oneself; ;
(un)curryThis
oneself
makes use of uncurryThis
, described here,
which is also available under oneself.uncurryThis
The companion, curryThis
, can be used to avoid that = this
:
var x = b: 'hi' { return thisb+' '+w+'!'; } a: oneself ; x;