Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I hate to answer my own question - seems like a bit of a faux pas - but c'est la vie. (because I woke up French today?)</p> <p>So, while I found that the eval() solution I presented last night in the edit to my original question does <em>seem</em> to be a valid solution, and a proper use of eval for retaining the object's context within the new function, it is far from perfect.</p> <p>Firstly, it works in FF, but both IE and Chrome seem to hate it (those were the next ones I tried, and I quit trying others after they both failed). Though I'm sure it could <em>probably</em> be made to work across browsers, it seems like a hassle.</p> <p>Secondly, it does give quite a bit of power to the new function, and as I look at my code more I do like the idea of controlling exactly what these new functions being added to my object get access to.</p> <p>Thirdly, .eval() is typically pretty slow - and it turns out that .apply() (which is typically faster) just may work well enough.</p> <p>This is because I realized at some point last night that no new functions on this object will need to set any of the private variables (at least, I'm fairly certain they won't) - and .apply() works fine to pass the values through for them to read.</p> <p>I'm sure there's more to it than just those 3 things, but for now I think I'm going to go with more of a 'wrapper' solution - something like this:</p> <pre><code>var f = function (){ var fauxThis = {}; fauxThis.priv = priv; obj1.fn2.apply(fauxThis, arguments); }; obj3.fn2 = f; //(To be placed where I had "obj3.fn2 = obj1.fn2;") </code></pre> <p>I am certainly willing now to consider the use of eval() in very specific cases - and may even revisit this specific use of it before I make my final decision of which direction to take. (especially if I can think of a case where the private value would need to be set)</p> <p>Thanks all for your input!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload