Note that there are some explanatory texts on larger screens.

plurals
  1. POCan you extend an object that has access to private properties with a function that can also access those private properties?
    primarykey
    data
    text
    <p>I am creating an object inside of an enclosure. Also in the enclosure are private properties that the object's functions can access - and this works as expected.</p> <p>My issue: I want others to be able to extend my object with functions of their own (functions from a different context), but those functions will also need access to the same private properties - and I have not been able to find a way to make this work.</p> <p>I've tried various configurations of .call, and also wrapping their function in a new function, amongst other things. I feel like I've gotten close to a solution, but have just fallen short.</p> <p>Here's a bit of simplified example code that accurately reflects my situation:</p> <pre><code>//extension object //fn2 can be any function, with any number of arguments, etc. var obj1 = {}; obj1.fn2 = function (s1, s2){ console.log(priv); }; //actual object var obj2 = (function (){ //private property var priv = "hello world"; //return object var obj3 = {}; //return object's native fn (works) obj3.fn = function (s){ console.log(priv); }; //extension happens here - but is obviously not correct obj3.fn2 = obj1.fn2; //return object return obj3; })(); //try output obj2.fn("goodbye world"); //works obj2.fn2("goodbye world", "thx 4 teh phish"); //fails </code></pre> <p>Any insight would be appreciated. And I totally understand if what I want just isn't possible - but it sure seems like it should be :P</p> <p><strong>EDIT:</strong> Thank you all for the responses. I fully understand that the properties are more easily accessed as public, and that normally inherited objects won't have access to them otherwise. However, since the new function is being attached to the original object I have to believe there's a way to use the original context and not the context the new function was created in.</p> <p>Now, I'm the first to say that eval is evil - and, in fact, I've <em>never</em> used it, or even considered using it, before. However, I'm trying everything I can think of to make this work - and I stumbled across this (seemingly) working solution:</p> <p>obj3.fn2 = eval(obj1.fn2.toString());</p> <p>So, if I check to make sure that obj1.fn2 is a typeof function, is there any way this could be harmful to my code? It doesn't execute the function, so I can't see how - but maybe I'm missing something?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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