helper libraries for lazy initialization of JS variables?

75 views
Skip to first unread message

Marak Squires

unread,
Dec 27, 2009, 7:04:39 AM12/27/09
to nod...@googlegroups.com
are there any good libraries out there for doing doing lazy initialization of JS variables and objects? pretty much syntax sugar for defining variables and default values. i have a small method here, "jset" which is really really basic. is there anything i can use in node to do this better?


// helper function for lazy initing objects
function jset( o , d ){
  if( typeof eval( o ) == 'undefined' ){ eval( o + ' = ' + d + ';' ); }
  return true;
}
 
// jset() usage
jset ( 'drip' , '{}' );
jset ( 'drip.value' , 42 );
jset ( 'drip.URI' , 'google.com' );




Tautologistics

unread,
Dec 27, 2009, 10:07:42 AM12/27/09
to nodejs

Well, there's this but it gets run offline to generate the final JS
code:

http://jashkenas.github.com/coffee-script/

As for the code you have, one can avoid the evals like so:

function Foo () {
}
Foo.prototype._counter = -1;
Foo.prototype.setCounter = function (value) { this._counter = value; }
Foo.prototype.name = "";
Foo.prototype.color = "";

function initObj (type, settings) {
var obj = new type();
for (var key in settings)
if ((typeof obj[key]) == "function")
obj[key](settings[key]);
else
obj[key] = settings[key];
return(obj);
}

var myFoo = initObj(Foo, {
name: "Bob",
color: "Green",
setCounter: 5
});

Joshaven

unread,
Dec 27, 2009, 1:39:39 PM12/27/09
to nodejs
I am not fully sure I understand what your wanting so my answer may
not be valid but here is what I would do with what I understand:

1) you want to ensure that your variable is defined.
2) if its not defined then you want to initialize it with a default
value.

var x = typeof(x) == "undefined" ? 42 : x;

On Dec 27, 7:04 am, Marak Squires <marak.squi...@gmail.com> wrote:

Marak Squires

unread,
Dec 27, 2009, 6:22:30 PM12/27/09
to nod...@googlegroups.com
err well the conditional assignment is just another way of implementing the basic functionality I posted with "jset", i'd want to add some additional functionality like type checking and validation

adding an additional argument, "type" you could start to do something like


// Pseudo code, won't run
function jset( o , d , t){
  if( typeof eval( o ) == 'undefined' ){ 
 switch(t)
case 'numeric' : //numeric valication
case 'date' : // date validation / formating logic
case 'URI' : //URI validation logic
case 'route' : //route validation logic

if(all_validation_tests_pass)
eval( o + ' = ' + d + ';' ); 
}
  return true;
}
 
// jset() usage
jset ( 'drip' , '{}' );
jset ( 'drip.value' , 42 , "numeric");
jset ( 'drip.startDate' , "12/29/2009 03:46:00" , "date");
jset ( 'drip.endDate' , "1/1/2010" , "date");
jset ( 'drip.route' , "/blah/path/to/resource" , "route");
jset ( 'drip.URI' , 'google.com', "URI" );



are there any projects out there that have similar functionality to this? i'm not trying to create a DSL.....just an easy to use variable setter for JS






--

You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.



Marak Squires

unread,
Dec 27, 2009, 7:08:35 PM12/27/09
to nod...@googlegroups.com
like duck typing in javascript I guess?

Isaac Z. Schlueter

unread,
Dec 27, 2009, 7:50:06 PM12/27/09
to nodejs
This seems like an overly clever hammer in search of a missing nail.

Isn't = an easy enough variable setter?

Eval is evil.

Note what happens when you try to do either of these:
jset("o", 1); // fails to set o to 1, because eval in scope of jset fn
e=== "o", not undefined
jset("pwned", {toString:function(){ return "\"pwned\""}); // surprise!
i just executed arbitrary code in your function setter!
jset("wtf", "arguments.callee");

Setting to a string would be tricky, since you have to quote it twice.

Not to mention, you're calling eval twice, and a string concatenation,
firing up a bunch of script parsers and whatnot, and created a load of
global leakage, and for what? Just doing this would be more robust,
faster, and less mysterious:

drip = drip || {};
drip.value = isNaN(+drip.value) ? 42 : +drip.value;
etc.

If you want to create many copies of an object from a single default
prototype, well, JavaScript has features to do this in an efficient
manner as well. Until the v8 team sees fit to implement
Object.create, you can use this:

Object.create = function (o) {
var f = function () {};
f.prototype = o;
return new f();
};
and go vote on http://code.google.com/p/v8/issues/detail?id=460

I'd toss the jset function in the "interesting curiousities" folder,
and move on to more useful things. It doesn't pull its complexity-
weight. For the love of all that is good and right in the world,
please don't go any further down this road.

--i

On Dec 27, 3:22 pm, Marak Squires <marak.squi...@gmail.com> wrote:
> err well the conditional assignment is just another way of implementing the
> basic functionality I posted with "jset", i'd want to add some additional
> functionality like type checking and validation
>

> adding an additional argument, "*t*ype" you could start to do something like


>
> // Pseudo code, won't run
> function jset( o , d , t){
>   if( typeof eval( o ) == 'undefined' ){
>     switch(t)
>      case 'numeric' : //numeric valication
>      case 'date' : // date validation / formating logic
>      case 'URI' : //URI validation logic
>      case 'route' : //route validation logic
>
>      if(all_validation_tests_pass)
>        eval( o + ' = ' + d + ';' );
>       }
>   return true;
>
> }
>
> // jset() usage
> jset ( 'drip' , '{}' );
> jset ( 'drip.value' , 42 , "numeric");
> jset ( 'drip.startDate' , "12/29/2009 03:46:00" , "date");
> jset ( 'drip.endDate' , "1/1/2010" , "date");
> jset ( 'drip.route' , "/blah/path/to/resource" , "route");
> jset ( 'drip.URI' , 'google.com', "URI" );
>
> are there any projects out there that have similar functionality to this?
> i'm not trying to create a DSL.....just an easy to use variable setter for
> JS
>

> > nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>

Marak Squires

unread,
Dec 28, 2009, 2:00:17 PM12/28/09
to nod...@googlegroups.com
isaac - 

can you actually read into what i'm asking and not just deliver clever punchlines and highlighting small pieces of pseudo code? 

i have already explicitly said all this code is a basic example of the CONCEPT i'm trying to find in an EXISTING library. evaluating the minutiae of the IMPLEMENTATION is pointless......

normally if you want to see if an object is of a "type" you run a function on it like is_numeric() or isValidDate() or isRoute()

i'm wondering if there are any libraries that bind this functionality with some lazy init stuff....so you can have default values and validators for variables. this becomes really really useful.....check out http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_p-q_01.html , CFML uses <CFPARAM> which : Tests for the existence of a parameter (that is, a variable), validates its data, and, if a default value is not assigned, optionally provides one.




To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.

inimino

unread,
Dec 29, 2009, 10:41:51 AM12/29/09
to nod...@googlegroups.com
Marak Squires wrote:
> isaac -
>
> can you actually read into what i'm asking and not just deliver clever
> punchlines and highlighting small pieces of pseudo code?

FWIW, I found the pseudo-code horrifying as well.

I would rather see some use cases, or at least pseudo-code of API use
rather than implementation.

> i'm wondering if there are any libraries that bind this functionality with
> some lazy init stuff...

What do you mean by lazy?

--
http://inimino.org/~inimino/blog/

Isaac Z. Schlueter

unread,
Dec 29, 2009, 9:47:00 PM12/29/09
to nodejs
Marak,

Ok, I must have misunderstood. What exactly are you trying to do?

Node is not very concerned with adding syntactic sugar to the
JavaScript language.

What you're describing is a bit like the Attribute and Base utilities
in YUI3. Of course, those are dealing with object properties, but
it's somewhat like what you're talking about with the CF stuff. Also,
YUI3 is a browser library, and not directly compatible with commonjs
or nodejs.
http://developer.yahoo.com/yui/3/attribute/
http://developer.yahoo.com/yui/3/base/

If you spelled out your use-case, it'd be easier to figure out a way
to get something like what you're after. Since you provided an
implementation, I figured that you were looking for a way to use that
implementation, but better. (As you put it, a way to "do this
better".)

Have fun!

--i

On Dec 28, 11:00 am, Marak Squires <marak.squi...@gmail.com> wrote:
> isaac -
>
> can you actually read into what i'm asking and not just deliver clever
> punchlines and highlighting small pieces of pseudo code?
>
> i have already explicitly said all this code is a basic example of the
> CONCEPT i'm trying to find in an EXISTING library. evaluating the minutiae
> of the IMPLEMENTATION is pointless......
>
> normally if you want to see if an object is of a "type" you run a function
> on it like is_numeric() or isValidDate() or isRoute()
>
> i'm wondering if there are any libraries that bind this functionality with
> some lazy init stuff....so you can have default values and validators for

> variables. this becomes really really useful.....check outhttp://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tag...

> > and go vote onhttp://code.google.com/p/v8/issues/detail?id=460

> > <nodejs%2Bunsu...@googlegroups.com<nodejs%252Buns...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages