Dynamic field name in object

M

machoq

Hi folks,
I have a javascript object called record and i want to pass the field
name dynamically. Something like
print(record.$field)

It always gives me null, what can i do so that it prints the value of
say record.filename when i pass $field="filename";

regards
-max
 
E

Erwin Moller

machoq said:
Hi folks,
I have a javascript object called record and i want to pass the field
name dynamically. Something like
print(record.$field)

It always gives me null, what can i do so that it prints the value of
say record.filename when i pass $field="filename";

regards
-max

Are you sure you posted to the right group?
Looks more like PHP to me.

Erwin Moller
 
S

SAM

machoq a écrit :
Hi folks,
I have a javascript object called record and i want to pass the field
name dynamically. Something like
print(record.$field)

what is this '$field' ?
it is something wrote by PHP, no ?
It always gives me null, what can i do so that it prints the value of
say record.filename when i pass $field="filename";

file 'my_example.php' :
<?
$field = 'Hello the world';
?>
<html>
<script type="text/javascript">
var record = new Object();
record.id = '1';
record.filename = '<?= $field ?>';
alert('record #'+record.id+' : '+record.filename);
</script>
</html>

do I expect
 
T

Thomas 'PointedEars' Lahn

Erwin said:
machoq said:
I have a javascript object called record and i want to pass the field
name dynamically. Something like
print(record.$field)

It always gives me null, what can i do so that it prints the value of
say record.filename when i pass $field="filename";
[...]

Are you sure you posted to the right group?
Looks more like PHP to me.

JFTR: The `$foo' notation is hacker jargon for any kind of arbitrary
substitution. http://en.wikipedia.org/wiki/Sigil_(computer_programming)


PointedEars
 
V

VK

Hi folks,
I have a javascript object called record and i want to pass the field
name dynamically. Something like
print(record.$field)

It always gives me null, what can i do so that it prints the value of
say record.filename when i pass $field="filename";

var record = ['filename':'foobar'];

var field = 'filename';

alert(record[field]); // 'foobar'
 
T

Thomas 'PointedEars' Lahn

VK said:
I have a javascript object called record and i want to pass the field
name dynamically. Something like
print(record.$field)

It always gives me null, what can i do so that it prints the value of
say record.filename when i pass $field="filename";

var record = ['filename':'foobar'];

The `['...`]' would mean an array initializer (ArrayLiteral), however `:'
is not allowed within and an array does not make sense here. Maybe you have
confused that with another language, however untested code should be marked
as such always.

Correct would be the following use of an object initializer (ObjectLiteral):

var record = {'filename': 'foobar'};

Since `filename' is an identifier, the apostrophes around it may be omitted:

var record = {filename: 'foobar'};
var field = 'filename';

alert(record[field]); // 'foobar'

alert() is a method of Window objects and should explicitly called
so instead of relying too much on the UA's scope chain:

window.alert(record[field]);


PointedEars
 
V

VK

var record = ['filename':'foobar'];

The `['...`]' would mean an array initializer (ArrayLiteral),

Right, a silly typo, I just was posting after some deep array
thinking :)
Correct would be the following use of an object initializer (ObjectLiteral):

var record = {'filename': 'foobar'};

Since `filename' is an identifier, the apostrophes around it may be omitted:

var record = {filename: 'foobar'};

Correct as long as identifier obeys to the language naming rules.
var field = 'filename';
alert(record[field]); // 'foobar'

alert() is a method of Window objects and should explicitly called
so instead of relying too much on the UA's scope chain:

window.alert(record[field]);

Stealing my ideas, you... :)
 
T

Thomas 'PointedEars' Lahn

VK said:
Correct as long as identifier obeys to the language naming rules.

What are you talking about? An identifier is an identifier as defined in
the Specification, and `filename' is one.


PointedEars
 
V

VK

What are you talking about? An identifier is an identifier as defined in
the Specification, and `filename' is one.

'filename' is one, 'old filename' or 'new filename' or 'filename[0]'
are not. This is what I'm saying: correct (skip on quotes) but only as
long as identifier obeys to the language naming rules so a valid
literal as defined in Javascript.
 
T

Thomas 'PointedEars' Lahn

VK said:
What are you talking about? An identifier is an identifier as defined in
the Specification, and `filename' is one.

'filename' is one, 'old filename' or 'new filename' or 'filename[0]'
are not. This is what I'm saying: correct (skip on quotes) but only as
long as identifier obeys to the language naming rules so a valid
literal as defined in Javascript.

There is no condition as to the correctness of my statement.


PointedEars
 
T

Thomas 'PointedEars' Lahn

VK said:
What are you talking about? An identifier is an identifier as defined in
the Specification, and `filename' is one.

'filename' is one, 'old filename' or 'new filename' or 'filename[0]'
are not.

Yes, but you missed the point.
This is what I'm saying: correct (skip on quotes) but only as
long as identifier obeys to the language naming rules so a valid
literal as defined in Javascript.

There is no condition to the correctness of my statement because the
statement already contains the required condition, and you are not making
any sense. "as long as identifier obeys to the language naming rules so a
valid literal as defined in Javascript" is incoherent gibberish.

Either it is an Identifier as defined by the grammar of the ECMAScript
specification, section A.1, or it is not. If it is not, then the
apostrophes or double quotes are required, otherwise they are not required.

Please get a new logic module before making further attempts at explanations.


PointedEars
 
E

Erwin Moller

Thomas said:
Erwin said:
machoq said:
I have a javascript object called record and i want to pass the field
name dynamically. Something like
print(record.$field)

It always gives me null, what can i do so that it prints the value of
say record.filename when i pass $field="filename";
[...]
Are you sure you posted to the right group?
Looks more like PHP to me.

JFTR: The `$foo' notation is hacker jargon for any kind of arbitrary
substitution. http://en.wikipedia.org/wiki/Sigil_(computer_programming)


PointedEars

JFTR: Elisabeth of Hungary was born on 7 july 1207.
http://en.wikipedia.org/wiki/Elisabeth_of_Hungary

Erwin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,082
Members
48,773
Latest member
Kaybee

Latest Threads

Top