By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,659 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,659 IT Pros & Developers. It's quick & easy.

Dynamic field name in object

P: n/a
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
Jan 11 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
machoq wrote:
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
Jan 11 '08 #2

P: n/a
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

--
sm
Jan 11 '08 #3

P: n/a
On Jan 11, 4:35*pm, machoq <sraiz...@gmail.comwrote:
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";
Bracket notation.

<URL: http://www.jibbering.com/faq/faq_not..._brackets.html >
Jan 11 '08 #4

P: n/a
VK
On Jan 11, 7:35 pm, machoq <sraiz...@gmail.comwrote:
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'
Jan 12 '08 #5

P: n/a
VK
On Jan 12, 7:30 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
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... :-)

Jan 12 '08 #6

P: n/a
VK wrote:
On Jan 12, 7:30 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>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.
What are you talking about? An identifier is an identifier as defined in
the Specification, and `filename' is one.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jan 12 '08 #7

P: n/a
VK
On Jan 12, 9:06 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
VK wrote:
On Jan 12, 7:30 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
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.

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.
Jan 12 '08 #8

P: n/a
VK wrote:
On Jan 12, 9:06 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>VK wrote:
>>On Jan 12, 7:30 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
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.
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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jan 12 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.