[PROPOSAL] "use" keyword

Michaël Rouges michael.rouges at gmail.com
Fri Jul 25 12:56:17 PDT 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140725/457302c0/attachment.html>


More information about the es-discuss mailing list