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 <[email protected]>
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
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss