Connecting Tech Pros Worldwide Forums | Help | Site Map

property alias in JavaScript?

petermichaux@gmail.com
Guest
 
Posts: n/a
#1: Jul 24 '06
Hi,

Is there a way to make one property an alias for another? This would
mean that if the value of the original is changed then so is the value
of the alias. I tried the following example and it does not give this
type of behavior, of course.

I don't think this aliasing is necessarily good in JavaScript but I may
need to present exactly the case that it doesn't work.

Thanks,
Peter

var foo = function(){
return "foo";
}

var alias = foo;

document.write(foo()); // foo
document.write(alias()); // foo

foo = function(){
return "changed";
}

document.write(foo()); // changed
document.write(alias()); // foo // if the alias variable really was
an alias this would output "changed"


petermichaux@gmail.com
Guest
 
Posts: n/a
#2: Jul 24 '06

re: property alias in JavaScript?


petermichaux@gmail.com wrote:
Quote:
Hi,
>
Is there a way to make one property an alias for another? This would
mean that if the value of the original is changed then so is the value
of the alias. I tried the following example and it does not give this
type of behavior, of course.
The following does what I want but maybe there is a better way?

Thanks,
Peter

var foo = function(){
return "foo";
}

var alias = function(){return foo.apply(null, arguments);};

document.write(foo());
document.write(alias());

foo = function(){
return "changed";
}

document.write(foo());
document.write(alias());

Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#3: Jul 24 '06

re: property alias in JavaScript?


petermichaux@gmail.com writes:
Quote:
The following does what I want but maybe there is a better way?
No. Variables and properties in Javascript cannot be aliased.

Your solution is not an alias, it's just another method that happen to
use the variable that you change. If you were to change "foo" to a
number, it would break.

Some browsers allow you to add triggers for property access and
modification, which can be used to duplicate the value in the nother
property, but that does not make it an alias, as can be seen by there
being points in time when the two properties are not in sync.
<URL:http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.5/reference/object.html#1193628>


/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
petermichaux@gmail.com
Guest
 
Posts: n/a
#4: Jul 25 '06

re: property alias in JavaScript?



Lasse Reichstein Nielsen wrote:
Quote:
petermichaux@gmail.com writes:
>
Quote:
The following does what I want but maybe there is a better way?
>
No. Variables and properties in Javascript cannot be aliased.
>
Your solution is not an alias, it's just another method that happen to
use the variable that you change. If you were to change "foo" to a
number, it would break.
>
Some browsers allow you to add triggers for property access and
modification, which can be used to duplicate the value in the nother
property, but that does not make it an alias, as can be seen by there
being points in time when the two properties are not in sync.
<URL:http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.5/reference/object.html#1193628>
Lasse, thanks for the info. I learned a lot by trying this and some
other variants of what I posted. Now that I've tried it I definitely
don't want to use it but it ended up being good practice with closures.

Peter

Closed Thread