passing variable to object

S

s k2999999

How can i pass the variable to object?
var test='hahaha';

callthis({required:true,test})
some how i wan to pass this variable to as a part of object.

S
 
S

SAM

Le 5/14/09 7:10 PM, s k2999999 a écrit :
How can i pass the variable to object?
var test='hahaha';

callthis({required:true,test})
some how i wan to pass this variable to as a part of object.


var o = {};

o.test = 'hahaha';

or :
var test = 'hahaha';
o.test = test;


Part(s) of object can be create at any time :

o.shchmilbick = o.test;
alert(o.shchmilbick);


Example:

JS:
function setPart(id, part, value) {
document.getElementById(id)[part] = value;
}

HTML:
<button
onclick="setPart('test', 'test', 'hohoho');
alert(document.getElementById('test').test)">
set part of div
</button>
<div id="test">
the div
</div>
 
R

RobG

Le 5/14/09 7:10 PM, s k2999999 a écrit :



var o = {};

o.test = 'hahaha';

or :
   var test = 'hahaha';
   o.test = test;

Part(s) of object can be create at any time :

Not "parts", *properties*.
    o.shchmilbick = o.test;
    alert(o.shchmilbick);

Example:

JS:
function setPart(id, part, value) {
   document.getElementById(id)[part] = value;

If the value of part is a DOM property name equivalent to a valid HTML
attribute name, that is OK, but do not use it to set other
properties.
   }

HTML:
<button
   onclick="setPart('test', 'test', 'hohoho');
            alert(document.getElementById('test').test)">

A better example would use a valid property, say className (which maps
to the HTML class attribute):

onclick="setPart('test', 'className', 'hohoho');
alert(document.getElementById('test').className)">
 
S

SAM

Le 5/15/09 4:39 AM, RobG a écrit :
Le 5/14/09 7:10 PM, s k2999999 a écrit :

var o = {};

o.test = 'hahaha';

or :
var test = 'hahaha';
o.test = test;

Part(s) of object can be create at any time :

Not "parts", *properties*.
Exacktelly.
o.shchmilbick = o.test;
alert(o.shchmilbick);

Example:

JS:
function setPart(id, part, value) {
document.getElementById(id)[part] = value;

If the value of part is a DOM property name equivalent to a valid HTML
attribute name, that is OK, but do not use it to set other
properties.

Certainly in absolute, but that can be done and that works.
You do not set a new attribute to the "DOM element" but add a new
property to the "object".
I don't know but perhaps would it be cleaner to create a new object
reprensenting the DOM element ?
var no = document.getElementById(id);
no.[part] = value;
return no;
A better example would use a valid property, say className (which maps
to the HTML class attribute):

Perhaps, but no.
My goal was to show that almost all things in JS are seen as objects.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top