It is not clear for me why you use eval in this example, please excuse me
if I mention very basic things for you not trying to insult nor judging
your 25 years as a developer, after all you can be new to js as I were few
months ago. I took the time to make this clear because you seem a little
bit susceptible in your responses in this thread. So help me undestand your
scenario and maybe we can both win

Why you do this:

var myobj = require( the-file );
eval( 'myobjs.' + file-name = myobj;');

Instead of:

myobjs[file-name] = require( the-file );


So then you can do

myobjs.filename.initialize()

I have seen this pattern quite a lot in node.js modules

El martes, 11 de diciembre de 2012, spqr escribió:

>
> Thanks for the thoughtful response.  My question was a bit "tongue in
> cheek."  I'm an experienced programmer and
> I've worked with a lot of languages and environments for over 25 years.  I
> started out my post saying that I was working
> on a program that is fairly unusual because the system I'm building is
> creating objects with code that gets executed
> and the loading and unloading of the modules is managed by the master
> program.
>
> I ran across "bind" and thought it might be a solution, but I hadn't
> experimented with it yet.
>
> In more detail, my scenario is this:
>
> Master program:
>
> Reads a directory containing JavaScript looping through each one and doing
> this:
>
> var myobj = require( the-file );
> eval( 'myobjs.' + file-name = myobj;');
>
> So, now I have an object 'myobjs' that has a reference to each object I've
> loaded.
>
> each file I'm loading looks something like this:
>
> module.exports = {
>           data1: "blah",
>           data2: "blah",
>           initialize: function(ctx, self) {
>                                 //your code here that can't access data1
> or data2 without self  :-)
>                         }
> };
>
> When I want to run one of the myobj.initialize functions, I'm currently
> doing this:
>
> var self = eval(myobjs.filename);
> eval(myobjs.filename.initialize)(myctx, self);
>
> This solution appears to work.
>
> On Tuesday, December 11, 2012 5:10:19 PM UTC-5, José F. Romaniello wrote:
>>
>> I agree with Brett, but Id like to add something to this:
>>
>> 2012/12/11 spqr <[email protected]>
>>
>>> Sure, I worked around it by getting a reference to the object I really
>>> want and passing it in to the
>>> function as an argument (annoying and ugly, but workable).
>>>
>>
>>
>> met 
>> Function.prototype.bind<https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/bind>.
>> You can basically use it as follows:
>>
>> function sayHello() {
>>   console.log('hello', this);
>> }
>> var helloFoo = sayHello.bind({foo: 'bar'});
>>
>> helloFoo(); //will print    hello {foo: 'bar'}
>>
>>   --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to 
> [email protected]<javascript:_e({}, 'cvml', '[email protected]');>
> To unsubscribe from this group, send email to
> [email protected] <javascript:_e({}, 'cvml',
> 'nodejs%[email protected]');>
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to