introduction of statefull functions (inspired by Scala language) - advanced specification of "operator()" for arbitrary objects

Jason Orendorff jason.orendorff at gmail.com
Thu May 12 19:02:36 UTC 2016


The nice thing about @@symbol-named methods is that we *don't* have to
extend the Proxy API for them. They're just method calls.

This is why there's no hasInstance Proxy trap. `instanceof` basically
boils down to a method call, and we already have Proxy traps for
method calls: `get` and `apply`.

It's a good thing, too, because adding a new Proxy trap is a last
resort. Just from a conceptual standpoint, the less complicated
objects are, the better; just because there are 14 *fundamental*
operations on objects doesn't mean we're eager to add more. But
there's also an inherent compatibility issue. Whenever you add a new
trap, the deal is, all existing Proxy handlers were written without
consideration for the new operation. So old code, used in combination
with the new language feature, would tend to break.

Cheers,
-j


On Thu, May 12, 2016 at 9:42 AM, Igor Baklan <io.baklan at gmail.com> wrote:
> It would be nice to "bring sense" to expressions like "obj(a1, ... , aN) =
> val".  (like "obj(x)  = y")
> In Scala langue it defined in pretty clear and simple way:
>
>    "obj(a1, ... , aN)" <==> "obj.apply(a1, ... , aN)"
>    "obj(a1, ... , an) = val" <==> "obj.update(a1, ... , aN, val)"
>
> Of course this applied only to that cases when obj was not defined like
> method (in case of regular methods "mth(args) = val" will cause compile time
> error).
> So in Scala even arrays and maps are accessed and updated using "()"
> operator
>
>    js( arr[index] = val ) <==> scala( arr(index) = val )
>
> (see http://www.scala-lang.org/api/2.10.0/index.html#scala.Array ,
> http://docs.scala-lang.org/overviews/collections/maps#operations-in-class-mutablemap
> , etc)
>
> So the proposals are:
>
> (1) to introduce symbols like @@apply and @@update with very similar to
> Scala meaning (for cases when 'obj.sttFunc' is not a function)
>
>   "obj.sttFunc(a1, ... , aN) = val" ==> "obj.sttFunc[Symbol.update]
> (obj.sttFunc, obj, [a1, ... , aN], val)"
>   "obj.sttFunc(a1, ... , aN)" ==> "obj.sttFunc[Symbol.apply] (obj.sttFunc,
> obj, [a1, ... , aN])"
>
> (2) to extend Prxoy object specification to support update action along with
> apply action:
>
> I would like to write something similar to
>
> var target = {
>    jsApply: function(key){
>       console.log("apply: ", key);
>       return key;
>    },
>    jsUpdate: function(key, val){
>       console.log("update: (", key, ") = ", val);
>       return val;
>    }
> };
> var sttFunc = new Proxy(target, {
>    apply: function(target, thisArg, argumentsList) {
>       target.jsApply.apply(thisArg, argumentsList);
>    },
>    update:  function(target, thisArg, argumentsList, assignValue) {
>       target.jsApply.apply(thisArg,
> Array.from(argumentsList).concat([assignValue]));
>    }
> });
>
> And then run it as following
>
>> sttFunc ("key") = "value";
>
> update: ( key ) =  value
>
>> sttFunc ("key");
>
> apply:  key
> "key"
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list