473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

property alias in JavaScript?

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
3 5301
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: FrankJeroen | last post by:
Hi there, I have a simple problem, but I can't fix it. I have worked with Visual Basic 6. But I recently switched to Visual Basic ..Net 2003. I have a Form with an Spreadsheet. I want to...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
0
by: David Lloyd | last post by:
When using Network.DNS.Resolve method (or any of trhe other resolution methods) the alias property only returns values when an actual alias is provided as the values to be resolved. Has anyone had...
19
by: laurenq uantrell | last post by:
I'm using Access 2K. I'm hoping someone can tell me which method performs faster- (currently I'm using a mix of both methods) a.) creating custom properties and then calling functions to set and...
2
by: Georg Mayer | last post by:
Hi, I am trying to display data from a DataSet in a Repeater Control. My item-template (just an example) is like this: <ItemTemplate> <td> <asp:Label Runat="server" Text='<%#...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
3
by: JakDaniel | last post by:
is it possible, create a selector as alias of another selector... (maybe) in another stylesheet file? ex: html page .... <link rel="stylesheet" type="text/css" href="style1.css" /> <link...
3
by: Jake G | last post by:
Hi, I have elements my page with an "alias" in them e.g. <input type=hidden id="S_SELLOG" name="S_SELLOG" iegAlias="S_SELLOG01" maxlength=1 title="Login/Logout" style="top:0; left:0;"> I am...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.