473,378 Members | 1,393 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,378 software developers and data experts.

change value of a Number object

Hi,

I'm hacking on a LISP interpreter in JS [1], and running into trouble
with numbers and Number objects.

This is what I have:

var a = new Number(1);
var b = a;
document.writeln(a == b); // true
b = 2;
document.writeln(a == b); // false
document.writeln(a); // 1

Instead of replacing the *Number object* b with the *number* 2, I'd like
to assign 2 to the value of the Number object, which should still be
referenced by b *and* a. In other words, the two last lines should
output "true" and "2".

Regards,
Stephan Walter
[1] http://www.parkscomputing.com/lisptest.html (not by me)
Aug 26 '05 #1
2 4165
On 26/08/2005 22:02, Stephan Walter wrote:

[a = new Number(1); b = a;]
Instead of replacing the *Number object* b with the *number* 2, I'd like
to assign 2 to the value of the Number object, which should still be
referenced by b *and* a.


You cannot. Number objects are immutable. You would have to create a new
Number object and reassign to both a and b (otherwise the other would
still reference the original).

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Aug 26 '05 #2
Stephan Walter <st*****@walter.name> writes:
var a = new Number(1);
var b = a; .... b = 2; Instead of replacing the *Number object* b with the *number* 2, I'd
like to assign 2 to the value of the Number object,


Sorry, can't be done. The [[Value]] property of a Number object cannot
be assigned to, but is fixed at the time the object is created.
(Maybe some browser out there has added a way to change the value, but
it's not in the ECMAScript standard).

If you want an object holding a value, you can make your own "number
box":

function Box(value) {
this.setValue(value);
}
Box.prototype.toString = function() { return String(this.value); };
Box.prototype.valueOf = function() { return this.value; };
Box.prototype.setValue = function(value) { this.value = Number(value); }
var a = new Box(42);
var b = a;
b.setValue(37);
alert(b==a);
alert(a)
/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.'
Aug 26 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to...
30
by: Alf P. Steinbach | last post by:
The C++ FAQ item 29.5 (this seems to be strongly related to C), at <url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5> mentions that <quote> C++ guarantees a char is exactly one...
8
by: deko | last post by:
I'm trying to find a way to set form/control properties programmatically. In a nut shell: 1. Open a database 2. Open a form in design view 3. Do something like this: For Each prp In...
0
by: Andy Eshtry | last post by:
I have a radio button list, a textbox representing SIN or EIN based on my radio button list selection so I put 2 regularexpressionvalidator to evaluate the value of textbox. EIN must be (for...
2
by: Greg Strong | last post by:
Hello All, Is it possible to change table field lookup properties in code? I've been able to change other field properties in code, however so far no luck with field lookup properties. What...
7
by: dotnetnoob | last post by:
i keep getting Object references not set to an instance of an object from this code: Private Sub EqBinding() Dim x As Integer x = 0 Do If CStr(arlsType.Item(x)) = "Bacnet Point" Then Dim...
2
by: dkl1 | last post by:
Hi, I have the following HTML code (for playing a WAV sound file): <OBJECT ID=vidCtrl_success CLASSID="CLSID:05589FA1-C356-11CE-BF01-00AA0055595A" STYLE="display:none" VIEWASTEXT> <PARAM...
1
by: Simon | last post by:
Dear reader, I am familiar with the possibility to change the Caption of a field in a form. But is there also a possibility to change the Caption of a field in the table structure it self.
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
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...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.