Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

References in javascript

How to duplicate the php &= operator



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

10+ Year Member

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?


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

10+ Year Member

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.


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

10+ Year Member

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

Purple Martin

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

WebmasterWorld Senior Member 10+ Year Member

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.



Featured Threads

Hot Threads This Week

Hot Threads This Month