[PROPOSAL] "use" keyword

Andrea Giammarchi andrea.giammarchi at gmail.com
Fri Jul 25 13:16:17 PDT 2014


function wrap(method) {
  return function () {
    var args = [].slice.call(arguments);
    args[0] = shared;
    return method.aply(this, args);
  };
}

foreignObject[property] = wrap(foreignObject[property]);

?

you still have the problem with the non writable methods ... but hey,
that's exactly the reason people make methods non writable .. you know, if
you don't own objects don't be that obtrusive ;-)


On Fri, Jul 25, 2014 at 12:56 PM, Michaël Rouges <michael.rouges at gmail.com>
wrote:

> Hi David.
>
> Sure... I make a library that that needs to share a private variable with
> all methods
> of an instance passed by arguments on my lib' method.
>
> ```
> var lib;
>
> (function () {
>     var shared;
>
>     shared = 'a shared value';
>
>     lib = {
>         method: function method(foreignObject) {
>             var property;
>
>             for (property in foreignObject) {
>                 if (foreignObject.hasOwnProperty('property') {
>                     if (typeof foreignObject === 'function') {
>                         // how to pass the "shared" variable
>                         // preserving the inside this in foreignObject
> methods
>                         // and without to touch the methods args?
>                     }
>                 }
>             }
>         }
>     };
> }());```
>
> The nearest way is something like this :
>
> ```
> var foreignObject;
>
> foreignObject = {
>     method: function method(shared) {
>         console.log(shared);
>     }
> };
>
> lib.method(foreignObject);
> ```
>
> And, in place of the lib.method comments :
>
> ```
> foreignObject[property] = foreignObject[property].bind(foreignObject,
> shared);
> ```
>
> It's really boring, because :
> - it don't works with non-writable methods,
> - it risks to loose the inside this of the methods (if not equal to
> foreignObject),
> - it forces to have a 'shared' arguments on each foreignObject method
>
>
> Better? :)
>
>
> Michaël Rouges - https://github.com/Lcfvs - @Lcfvs
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140725/35881711/attachment-0001.html>


More information about the es-discuss mailing list