Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-05-2012, 06:16 AM   PM User | #1
qwertyuiop
Regular Coder

 
Join Date: Jan 2004
Location: USA
Posts: 364
Thanks: 12
Thanked 6 Times in 6 Posts
qwertyuiop is an unknown quantity at this point
Problem with passing object as argument

PHP Code:
function $(x) {
  return 
document.getElementById(x);
}

function 
test(x) {
  
alert(x.id);
}

function 
init() {
  var 
= {};

  
o.id "a";
  $(
"one").addEventListener("click", function () {test(o);});

  
o.id "b";
  $(
"two").addEventListener("click", function () {test(o);});

  
o.id "c";
  $(
"three").addEventListener("click", function () {test(o);});
}

window.onload init
I have some code similar to this. When the HTML elements with id's "one", "two", or "three" are clicked, they all alert "c", but I want them to alert the respective values (a, b, and c).

How can I pass the object by value and not by reference? I guess in this simple example you would just define two more objects, but my code is actually within a for-loop. The object properties are changed each iteration (ideally each iteration creates a new object) and then passed to the function.
__________________
Running Windows 7 x64
qwertyuiop is offline   Reply With Quote
Old 01-05-2012, 06:52 AM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Your object 'o' persists in the closure, so its last-assigned value is always read.
One way could be to do this:
Code:
function init() 
{
  var t = [ 'a', 'b', 'c' ];

  $("one").addEventListener("click", function () {test( t[0] );});


  $("two").addEventListener("click", function () {test( t[1] );});


  $("three").addEventListener("click", function () {test( t[2] );});
}
Logic Ali is offline   Reply With Quote
Old 01-05-2012, 07:25 AM   PM User | #3
qwertyuiop
Regular Coder

 
Join Date: Jan 2004
Location: USA
Posts: 364
Thanks: 12
Thanked 6 Times in 6 Posts
qwertyuiop is an unknown quantity at this point
Thanks Logic Ali. I guess I simplified my example a bit too much though, or wasn't clear enough. The function test takes an object as a parameter, not just a simple string.
__________________
Running Windows 7 x64
qwertyuiop is offline   Reply With Quote
Old 01-05-2012, 07:38 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You can't do it.

You are under the mistaken impression that there are three (or four) objects in use.

NOT TRUE.

JavaScript *ALWAYS* passed object by REFERENCE. You can never actually hold an object in a variable.

Now, if you REDEFINED the object between adding each listener, you could do it with a closure.
Code:
<html>
<head>
<script>
function $(x) {
  return document.getElementById(x);
}

function test(x) {
  alert(x.id);
}

function addClickTo( towhat, withwhat )
{
    towhat.addEventListener("click", function( ) { test(withwhat); } );
}

function init() {
  var o = { id: "a" };
  addClickTo( $("one"), o );


  o = { id: "b" };
  addClickTo( $("two"), o );

  o = { id: "c" };
  addClickTo( $("three"), o );

}

window.onload = init;  
</script>
</head>
<body>
<input type="button" id="one" value="one"/>
<input type="button" id="two" value="two"/>
<input type="button" id="three" value="three"/>
</body>
</html>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 01-05-2012 at 07:43 AM..
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
qwertyuiop (01-05-2012)
Old 01-05-2012, 07:43 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I guess the other way to do it would be to clone the object as part of the closure function:
Code:
<script>
function $(x) {
  return document.getElementById(x);
}

function test(x) {
  alert(x.id);
}

function addClickTo( towhat, withwhat )
{
    var usethis = { }
    /* write code here to clone withwhat into usethis */
    towhat.addEventListener("click", function( ) { test(usethis); } );
}

function init() {
  var o = { };
  o.id = "a";
  addClickTo( $("one"), o );

  o.id = "b";
  addClickTo( $("two"), o );

  o.id = "c";
  addClickTo( $("three"), o );

}

window.onload = init;  
</script>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:31 AM.


Advertisement
Log in to turn off these ads.