Why are you using eval?
--
Pedro
On Tuesday, 11 de December de 2012 at 23:15, spqr wrote:
>
> 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]
> 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
--
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