property alias in JavaScript?

P

petermichaux

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"
 
P

petermichaux

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());
 
L

Lasse Reichstein Nielsen

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
 
P

petermichaux

Lasse said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top