// helper function for lazy initing objectsfunction jset( o , d ){if( typeof eval( o ) == 'undefined' ){ eval( o + ' = ' + d + ';' ); }return true;}// jset() usagejset ( 'drip' , '{}' );jset ( 'drip.value' , 42 );jset ( 'drip.URI' , 'google.com' );
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
});
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:
// Pseudo code, won't runfunction jset( o , d , t){
if( typeof eval( o ) == 'undefined' ){
switch(t)case 'numeric' : //numeric valicationcase 'date' : // date validation / formating logiccase 'URI' : //URI validation logiccase 'route' : //route validation logicif(all_validation_tests_pass)
eval( o + ' = ' + d + ';' );}return true;}// jset() usagejset ( '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" );
--
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.
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>
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
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?
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>