Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  72] [ 3]  / answers: 1 / hits: 43858  / 12 Years ago, sat, october 27, 2012, 12:00:00

For example, can I do this?:



{ 
a: b: c: d: 1,
e: 2,
geh: function() { alert(Hi!) }
}


EDIT:
Is there some way I can avoid doing this?:



{ 
a: 1,
b: 1,
c: 1,
d: 1,
e: 2,
geh: function() { alert(Hi!) }
}

More From » properties

 Answers
32

You could set a line of equality between various properties:



var foo = {};
foo.a = foo.b = foo.c = Hello;


Or you could just create a method that does the mass-assignment for you:



var foo = {
setValue: function( props, value ) {
while ( props.length ) this[ props.pop() ] = value;
}
}

foo.setValue( [ a, b, c ] , Foo );

[#82316] Thursday, October 25, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breanab

Total Points: 731
Total Questions: 95
Total Answers: 79

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
;