homepage Welcome to WebmasterWorld Guest from 54.198.8.124
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
References in javascript
How to duplicate the php &= operator
tulip




msg:1483132
 5:50 pm on Oct 1, 2002 (gmt 0)

Is there a way to make references in javascript to variables in same matter as php, e.g.


var a = 2;
var b = a;
var c &= a;

a += 2;
alert(b) // result: 2;
alert(c) // result: 4;


I could obviously create a custom object and attach the required data as properties of the object (javascript seems to have object references), but other than objects?

ideally what I want to do is something like this:

var b = 200;
var a = document.getElementById('someelement');
var c = document.getElementById('anotherelement');
var d = document.getElementById('a3rd');

a.style.height &= b/4;
c.style.width &= b*2;
d.style.height &= (0.5*b)/a;

..
..
b=100 // the 3 elements automatically resizing without having to
// assign the values to each element all over again


In php it is so easy with the reference operator (&=) but I am still looking for something similar in javascript.

Good ideas?

 

toadhall




msg:1483133
 6:52 pm on Oct 1, 2002 (gmt 0)

Hi tulip (nice name), welcome to Webmaster World.

There is a bitwise operator & and a related assignment operator &= in Javascript.
It performs in the same way as in php.

Here's O'Reilly on the Javascript:

Bitwise And (&)
The & operator performs a boolean AND operation on each of its integer arguments. A bit is set in the result only if the corresponding bit is set in both operands.

...and the php manual [php.net]:

Table 11-3. Bitwise Operators
Example: $a & $b
Name: And
Result: Bits that are set in both $a and $b are set

I have no experience using bitwise operators so that's as far as I can help. Perhaps you could explain how you're using them. I've done some image size manipulation for formatting a table of thumbnails according to screen resolution using Javascript but my calculation method used only logical operators.

tulip




msg:1483134
 12:07 am on Oct 2, 2002 (gmt 0)

I was not referring to the bitwise operator, but variable references (I showed the php example from php.net).

Purple Martin




msg:1483135
 1:07 am on Oct 2, 2002 (gmt 0)

You can set a JavaScript variable to any value you like, including a reference to any DOM object. However, JavaScript variables are not objects themselves, so you can't set a variable to a reference to another variable.

You can only set a variable using = or +=. Once you set the value, it stays exactly the same until you set it to something else.

HTH

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved