By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,914 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,914 IT Pros & Developers. It's quick & easy.

property alias in JavaScript?

P: n/a
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"

Jul 24 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
pe**********@gmail.com wrote:
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());

Jul 24 '06 #2

P: n/a
pe**********@gmail.com writes:
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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 24 '06 #3

P: n/a

Lasse Reichstein Nielsen wrote:
pe**********@gmail.com writes:
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

Jul 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.