How do you assign default object parameters?

Hello again! :slight_smile:

Do you…

function myfunc(config:Object){
 if(!config.url) config.url = "http://www.google.com/";
 if(!config.target) config.target = "_blank";  
}

…and so on for each parameter?

Am I missing a simpler way to do this?

Regards!

Well, if you use this kind of Object alot one way you could do this is to create a class for it. It could be a simple small one that has all of the properties you need and does all the value defaulting inside the class. That’d be the way I would do this anyway.

Of course if you only do this in one place a class might not be worth it. In that case, no I don’t know any other way to default the parameters of an object.

Thanks!

The class approach is a good idea. Not for this time, but I’ll use that for sure.

may the force be with you :slight_smile:

Slightly shorter:

var o:Object = new Object();
o.prop ||= 'hi';
o.prop ||= 'hey';
trace(o.prop); // hi

[QUOTE=Krilnon;2346088]Slightly shorter:

var o:Object = new Object();
o.prop ||= 'hi';
o.prop ||= 'hey';
trace(o.prop); // hi

[/QUOTE]

Hmmm looks interesting, never thought of using the OR operator this way.

How does it work exactly?

http://blogs.adobe.com/fcheng/2008/02/logical_assignment_operators.html