Equivalent of Strcomp

A

Andrew Poulos

I'm writing some ASP using js and I need to do a case sensitive SQL
select. Googling gave me this:

SELECT * FROM User WHERE Strcomp("Blue",[Password],vbBinaryCompare)=0

Strcomp is from vbs. Is there a js equivalent, or some other way to
handle this?

Andrew Poulos
 
B

Bart Van der Donck

Andrew said:
I'm writing some ASP using js and I need to do a case sensitive SQL
select. Googling gave me this:

SELECT * FROM User WHERE Strcomp("Blue",[Password],vbBinaryCompare)=0

Strcomp is from vbs. Is there a js equivalent, or some other way to
handle this?

As long as we're talking about binary comparisons (StrComp's default,
zero), then I think the following should so the trick:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>javascript binary Strcomp</title>
</head>
<body>
<table border="1">
<tr><th>VBS</th><th>JS</th></tr>
<tr><td>
<script language="vbscript" type="text/vbscript">
document.write(Strcomp("Blue","blue",0))
document.write("&nbsp;<BR>")
document.write(Strcomp("blue","blue",0))
document.write("&nbsp;<BR>")
document.write(Strcomp("blue","Blue",0))
document.write("&nbsp;<BR>")
document.write(Strcomp("blues","Blue",0))
document.write("&nbsp;<BR>")
document.write(Strcomp("123","abc",0))
document.write("&nbsp;<BR>")
document.write(Strcomp("foo","bar",0))
document.write("&nbsp;<BR>")
document.write(Strcomp("0","01",0))
</script>
</td>
<td>
<script language="javascript" type="text/javascript">
function js_Strcomp_binary(v1,v2) {
if (v1>v2) nr = 1
else if (v1==v2) nr = 0
else if (v1<v2) nr = -1
document.write(nr + "&nbsp;<BR>")
}
js_Strcomp_binary("Blue","blue")
js_Strcomp_binary("blue","blue")
js_Strcomp_binary("blue","Blue")
js_Strcomp_binary("blues","Blue")
js_Strcomp_binary("123","abc")
js_Strcomp_binary("foo","bar")
js_Strcomp_binary("0","01")
</script>
</td></tr>
</table>
</body>
</html>

Hope this helps,
 
B

Bart Van der Donck

Bart said:
[...]
function js_Strcomp_binary(v1,v2) {
if (v1>v2) nr = 1
else if (v1==v2) nr = 0
else if (v1<v2) nr = -1

Oops, I forgot to add the following here:

if (!v1||!v2) nr = null
 
V

VK

Bart said:
Bart said:
[...]
function js_Strcomp_binary(v1,v2) {
if (v1>v2) nr = 1
else if (v1==v2) nr = 0
else if (v1<v2) nr = -1

Oops, I forgot to add the following here:

if (!v1||!v2) nr = null
document.write(nr + "&nbsp;<BR>")
}

It worth mention that String on modern UAs has native localeCompare
method which is especially priceless for non-ASCII strings comparison.
Besides that it returns shuttle value (-1, 0, 1) by default:

var a = 'abcd';
var b = 'efgh';
alert(a.localeCompare(b));

IE and Firefox both support it.

P.S. Where is the snow? Winter in Europe... you must be kidding... if I
knew I would go to Tahoe... :) :-(
 
B

Bart Van der Donck

VK said:
Bart said:
Bart said:
[...]
function js_Strcomp_binary(v1,v2) {
if (v1>v2) nr = 1
else if (v1==v2) nr = 0
else if (v1<v2) nr = -1

Oops, I forgot to add the following here:

if (!v1||!v2) nr = null
document.write(nr + "&nbsp;<BR>")
}

It worth mention that String on modern UAs has native localeCompare
method which is especially priceless for non-ASCII strings comparison.
Besides that it returns shuttle value (-1, 0, 1) by default:

var a = 'abcd';
var b = 'efgh';
alert(a.localeCompare(b));

Except that it doesn't return null if one of the two strings is
undefined:

javascript:
alert('abc'.localeCompare(null)) // says '-1'

vbscript:
alert(Strcomp("abc",null)) // says 'null'
 
R

Richard Cornford

VK said:
Bart said:
Bart said:
[...]
function js_Strcomp_binary(v1,v2) {
if (v1>v2) nr = 1
else if (v1==v2) nr = 0
else if (v1<v2) nr = -1

Oops, I forgot to add the following here:

if (!v1||!v2) nr = null

Empty strings are valid subjects for comparison so null should not be
returned if either string is empty. This is a case where
type-converting comparison (==) with null is the appropriate test as
both null and undefined equal null, but no string ever would.
It worth mention that String on modern UAs has native localeCompare
method which is especially priceless for non-ASCII strings comparison.

When all javascript strings are internally 16 bit code points it makes
no difference to a comparison whether the characters are non-ASCII or
not.

The - localCompare - method takes into account location dependent
ordering rules while doing text-based comparisons. The Strcomp function
without a third argument does binary comparisons. Thus - localCompare -
is an inappropriate suggestion as javascript's normal string comparison
by code point value is the required behaviour.
Besides that it returns shuttle value (-1, 0, 1) by default:

It returns implementation dependent values where equality being zero
and the sign of non-equal values is all that is specified.
var a = 'abcd';
var b = 'efgh';
alert(a.localeCompare(b));

IE and Firefox both support it.
<snip>

The - localCompare - method is specified in ECMA 262, 3rd Ed. so all
3rd Edition conforming implementations will support it, including IE
and Firefox.

Richard.
 
B

Bart Van der Donck

Richard said:
Empty strings are valid subjects for comparison so null should not be
returned if either string is empty. This is a case where
type-converting comparison (==) with null is the appropriate test as
both null and undefined equal null, but no string ever would.

I'm surprised that it catches the empty string as well in javascript
[*]. For the record,

if (!v1||!v2) nr = null

must become

if (v1 == null || v2 == null) nr = null

[*] IMHO, "if (v1)" should logically read "if v1 is defined", and not
"if v1 isn't empty". Variables that hold an empty value, have been
assigned a defined value, namely the empty one (an empty bottle is also
a bottle, it just happens to be empty). IMVHO. Maybe it's an influence
from other languages.
 
R

Richard Cornford

Bart Van der Donck wrote:
[*] IMHO, "if (v1)" should logically read "if v1 is defined", and not
"if v1 isn't empty".

It isn't either, it is "if the value of v1 has true-ness".
(Incidentally, if - v1 - was not defined (as a formal parameter for the
function) the code would error-out)
Variables that hold an empty value, have been
assigned a defined value, namely the empty one (an empty bottle
is also a bottle, it just happens to be empty). IMVHO. Maybe it's an
influence from other languages.

Possibly. In a langue where values will be type-converted it is
important to know what values convert to which values of other types
(and, in the case of objects, the calling of which methods determines
those values).

Richard.
 
V

VK

Besides that it returns shuttle value (-1, 0, 1) by default:
Except that it doesn't return null if one of the two strings is
undefined:

It cannot by definition: it will be either a shuttle value (-1, 0, 1)
or error if the operand is null or not an object. localeCompare imposes
string context on arguments, so implicit (toString) casting is used
before the comparison.
(toString)null == "null"
this way
var v1 = 'null';
var v2 = null;
alert(v1.localeCompare(v2));
gives 0 (equal strings) because "null" and "null" are being compared.

At the same time
var v1 = null;
var v2 = 'null';
alert(v1.localeCompare(v2));
will produce run-time error because indeed you cannot use
localeCompare() method on null;
javascript:
alert('abc'.localeCompare(null)) // says '-1'

thus "lesser than argument" which is right: "abc" string is lesser (in
the string comparison sense) than "null" string;
alert('nulm'.localeCompare(null)) // will give you 1
vbscript:
alert(Strcomp("abc",null)) // says 'null'

Here comes to differences of Nothing, Empty and Null in VB family.
Overall any operation results with imaginary values are "contract
based": they are whatever was decided for a given language. Say there
is nothing in NaN "nature" that would require it be not equal to
anything including itself. It is just a contract behavior. The same
applies to undefined (Nothing) and null (Null).
This is why it is very difficult sometimes to get _exactly_ the same
behavior as in some other language: because one doesn't have control
over contract behavior over imaginary values.
 
B

Bart Van der Donck

VK said:
It cannot by definition: it will be either a shuttle value (-1, 0, 1)
or error if the operand is null or not an object.

Then localeCompare is not a suitable equivalent for Strcomp , which
gets us back at the beginning of this thread. Strcomp always returns
null if any of the two strings (or both) are null.
[...]
vbscript:
alert(Strcomp("abc",null)) // says 'null'

Here comes to differences of Nothing, Empty and Null in VB family.
Overall any operation results with imaginary values are "contract
based": they are whatever was decided for a given language. Say there
is nothing in NaN "nature" that would require it be not equal to
anything including itself. It is just a contract behavior. The same
applies to undefined (Nothing) and null (Null).
This is why it is very difficult sometimes to get _exactly_ the same
behavior as in some other language: because one doesn't have control
over contract behavior over imaginary values.

I don't think that "imaginary values" is a right wording here. But I
share the same experience with such assignments, yes (see only in this
thread the other posts about null/empty/undefined).
 
B

Bart Van der Donck

Richard said:
It isn't either, it is "if the value of v1 has true-ness".
(Incidentally, if - v1 - was not defined (as a formal parameter for the
function) the code would error-out)

Interesting definition. But it also implies that (defined) emptiness
holds no true-ness. I would rather say that the true-ness of an empty
value lies in the fact that it was computer-scientifically assigned,
defined, known, ... So, in that view, the empty value has equally the
same true-ness as any other assigned value; it's just empty, identical
to a non-empty string, but then empty. It's the assignment that makes
the value of the variable defined and lifts it out of the dark where
nothing was said/known about the value (just the name of the var was
reserved).

The code only errors out if there was no (prior) declaration of the
variable, as in any language that is somewhat strict about its
declarations. An assignment (if any) gives a value to the variable
after its declaration. When a value is assigned (which might be the
empty value, like x = ""), I would say it logically starts holding
true-ness from that very moment.

I can find myself in your view that 'null' or 'undefined' hold no
true-ness in their values.
 
V

VK

Bart said:
Then localeCompare is not a suitable equivalent for Strcomp , which
gets us back at the beginning of this thread. Strcomp always returns
null if any of the two strings (or both) are null.

Yes, that is VB(A) choice for comparison of special values. That
suggests to avoid implicit data conversions for "bridging" methods:
thus where the result is obtained in one environment and then sent to
another environment where the result would be possibly different even
with the same arguments. Instead one should use explicit type check
before the operation. This way it is possible to emulate javascript or
VB or whatever needed:

function StrComp(s1, s2) {
if ((typeof s1 == 'undefined') || (typeof s2 == 'undefined')) {
return null;
}
// both null and object reported as "object"
// so two separate branches:
else if ((s1 == null) || (s2 ==null)) {
return null;
}
// I do not remember what does VB return in this case
else if ((typeof s1 == 'object') || (typeof s2 =='object'))
return "?";
}
// I do not remember what does VB return in this case
else if ((typeof s1 == 'function') || (typeof s2 =='function'))
return "?";
}
// I do not remember what does VB return in this case
else if ((typeof s1 == 'boolean') || (typeof s2 =='boolean'))
return "?";
}
else {
return s1.localeCompare(s2);
}

That gets a bit paranoidal of course: as it presumes a user targeted to
crash the program rather than to use it. My position on this aspect was
always "democratic": whoever really applies himself to get a run-time
error - so let him have it. Maybe he he will finally read the fn manual
after that :)

For the OP case I'm more concerned of null value returned from
anywhere. By the question context I presume it is for some server-side
DB processing which means that all nulls will be eventually transformed
into string values: either for form submission or for ajaxoid. How will
they transformed, will the transformation succeed, what the DB driver
will get and is it ready for say "null" string in client response
meaning Nothing in "its world"?
I don't think that "imaginary values" is a right wording here.

I agree, that may bring to much of "parasite context" of the math. But
the conventional "special values" term is universal up to being
meaningless: it can mean anything anywhere.

Any better terms?
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Fri, 5 Jan 2007 02:39:53, Bart Van der Donck
function js_Strcomp_binary(v1,v2) {
if (v1>v2) nr = 1
else if (v1==v2) nr = 0
else if (v1<v2) nr = -1
document.write(nr + "&nbsp;<BR>")
}


A) If one has knowledge of the likely results, it may be worth re-
ordering those tests.

B) This should be equivalent, and always does 4 comparisons :
nr = v1>v2 - v1<v2 // if I have the details right.
 
R

Richard Cornford

Bart said:
Interesting definition.

That is not a definition. It is significant that the subject of your
test is a _value, the value held by the property of the variable object
with the name that corresponds with the formal parameter - v1 -, and
that the value will be type-converted to boolean (which is javascript's
criteria for true-ness).
But it also implies that (defined) emptiness
holds no true-ness.

What is emptiness in this context? If no corresponding argument is
passed to the function call the formal parameters are defaulted to the
undefined value (the single value of the Undefined type, which
type-converts to boolean false).
I would rather say that the true-ness of an empty
value lies in the fact that it was computer-scientifically
assigned, defined, known,

Doesn't that suggest that when a boolean false value is assigned you
would have it considered true?
... So, in that view, the empty value has equally the
same true-ness as any other assigned value;

Including the value boolean false?
it's just empty, identical to a non-empty string, but
then empty.

Are you now not talking of values in general but inserted talking only
of string primitive valves? It would, in principle, be possible for all
string primitives to type-convert to boolean true. but that is not the
way it is in javascript.
It's the assignment that makes
the value of the variable defined

No it is not. Variables are defined when Identifiers are used with the -
var - keyword, used as formal parameters in function declarations and
used as the names of declared functions.
and lifts it out of the dark where nothing was said/known
about the value (just the name of the var was reserved).

If a variable is declared and no other value assigned to it then it
_explicitly_ has the undefined value. Thus any actual assignment makes
no difference to its status as far as 'having a value' goes. This is
particularly obvious when you realise that it is possible to explicitly
assign the undefined value to a variable. If it had the undefined value
prior to the assignment before that assignment, and has the undefined
value following the assignment, in what sense could the assignment be
said to "lifts it out of the dark"?
The code only errors out if there was no (prior) declaration
of the variable,

The 'variable' is a function's formal parameter, so it was 'declared'
(and also has been assigned a value).
as in any language that is somewhat strict about its
declarations.

In that respect javascript is probably one of the least strict languages
going.
An assignment (if any) gives a value to the variable
after its declaration.

In javascript all variables (and formal parameters and declared
functions) are given values during variable instantiation. For a
variable that value is the undefined value, for a formal parameter the
value corresponds with the an argument to the function call, or defaults
to undefined if no argument is received (and the values of declared
functions are references to function objects, unsurprisingly).
When a value is assigned (which might be the
empty value, like x = ""

That is an empty stirring (a string primitive containing no characters),
but it would be a bit hard to call it an "empty value". For a start
there is no concept of an "empty value" in javascript, but an empty
string also exists and has type (it is a string primitive).
), I would say it logically starts holding
true-ness from that very moment.

True-ness in javascript is the measure of how a value will be handled in
a boolean context. Thus if follows from with of the two boolean values
any other value will be type-converted to if used in a context where
such a type-conversion is implied.
I can find myself in your view that 'null' or 'undefined'
hold no true-ness in their values.

They both type-convert to boolean false if used in a context where such
a type-conversion is implied.

Richard.
 
B

Bart Van der Donck

Richard said:
That is not a definition. It is significant that the subject of your
test is a _value, the value held by the property of the variable object
with the name that corresponds with the formal parameter - v1 -, and
that the value will be type-converted to boolean (which is javascript's
criteria for true-ness).


What is emptiness in this context? If no corresponding argument is
passed to the function call the formal parameters are defaulted to the
undefined value (the single value of the Undefined type, which
type-converts to boolean false).


Doesn't that suggest that when a boolean false value is assigned you
would have it considered true?


Including the value boolean false?


Are you now not talking of values in general but inserted talking only
of string primitive valves? It would, in principle, be possible for all
string primitives to type-convert to boolean true. but that is not the
way it is in javascript.


No it is not. Variables are defined when Identifiers are used with the -
var - keyword, used as formal parameters in function declarations and
used as the names of declared functions.


If a variable is declared and no other value assigned to it then it
_explicitly_ has the undefined value. Thus any actual assignment makes
no difference to its status as far as 'having a value' goes. This is
particularly obvious when you realise that it is possible to explicitly
assign the undefined value to a variable. If it had the undefined value
prior to the assignment before that assignment, and has the undefined
value following the assignment, in what sense could the assignment be
said to "lifts it out of the dark"?


The 'variable' is a function's formal parameter, so it was 'declared'
(and also has been assigned a value).


In that respect javascript is probably one of the least strict languages
going.


In javascript all variables (and formal parameters and declared
functions) are given values during variable instantiation. For a
variable that value is the undefined value, for a formal parameter the
value corresponds with the an argument to the function call, or defaults
to undefined if no argument is received (and the values of declared
functions are references to function objects, unsurprisingly).


That is an empty stirring (a string primitive containing no characters),
but it would be a bit hard to call it an "empty value". For a start
there is no concept of an "empty value" in javascript, but an empty
string also exists and has type (it is a string primitive).


True-ness in javascript is the measure of how a value will be handled in
a boolean context. Thus if follows from with of the two boolean values
any other value will be type-converted to if used in a context where
such a type-conversion is implied.


They both type-convert to boolean false if used in a context where such
a type-conversion is implied.

I have absolutely no doubt that you are correct about your descriptions
in javascript, but I do doubt if I understand it all :)

In the traditional theory, each variable must always be undefined
before it can get a value:

(1) reserve the name (declaration) [*]
(2) assign a value, that is, populate bytes with bit patterns
(3) do (2) again as much as you want (or make it undefined again (free
up the memory))

Note that no step is mandatory. Emptiness would traditionally take
place in (2), because it is an explicit act of assigning "" to the
previously declared name. It appears that javascript doesn't follow
this logic. Even "var x = 0; if (x) {alert('ok') }" doesn't show the
alert-window.

Probably I'm thinking in too classic theoretic patterns.
 
B

Bart Van der Donck

VK said:
I agree, that may bring to much of "parasite context" of the math. But
the conventional "special values" term is universal up to being
meaningless: it can mean anything anywhere.

Any better terms?

I feel something for "the abstract conditionedness of a variable"

(cheers!)
 
R

Richard Cornford

Bart Van der Donck wrote:
I have absolutely no doubt that you are correct about your
descriptions in javascript, but I do doubt if I understand
it all :)

So long as you decline to answer the questions you are asked nobody else
can judge how much you do or don't understand.
In the traditional theory,

Which "traditional theory"?
each variable must always be undefined
before it can get a value:

Wouldn't that depend a great deal of what you men by a "variable" and by
"undefined"? In javascript variables are properties of object (and
properties of objects always have values in javascript) and undefined is
a value (the single value of the Undefined type). There is no sense in
which a javascript variable can exist and not have a value, or be
"undefined" before it has a value.
(1) reserve the name (declaration) [*]

There is no reserving of names in javascript. The properties of objects
that represent variables are created during "variable instantiation".
(2) assign a value, that is, populate bytes with bit patterns

The nature of assigning values in javascript is not certain, nor
important.
(3) do (2) again as much as you want (or make it undefined again

Assigning the undefined value to a property of an object is not distinct
from assigning any other value.
(free up the memory))

In what sense "free up the memory"? The object that has the property
retains its property, and whatever structure links that property with
its value must continue to exist if the value that is linked to is the
undefined value.
Note that no step is mandatory. Emptiness would traditionally
take place in (2), because it is an explicit act of assigning
"" to the previously declared name.

Again you appear to be attributing some wider concept of "emptiness" to
a single value of the string primitive type (the empty string).
It appears that javascript doesn't follow this logic.

Javascript has well specified behaviour that is both consistent and
logical.
Even "var x = 0; if (x) {alert('ok') }" doesn't show the
alert-window.

Why is this relevant? The expression for an - if - statement is
type-converted to boolean in order to decide what it is going to do, and
numeric zero (and NaN) type-convert to boolean false. But why would this
be 'theoretically' unexpected anyway, as in C numeric zero is regarded
as the false value?
Probably I'm thinking in too classic theoretic patterns.

Or maybe you are trying to apply a theory to the wrong structures.
[*] Maybe javascript's "var myvar;" syntax is a far heir of that.

That does not make sense to me.

Richard.
 
B

Bart Van der Donck

Richard said:
Wouldn't that depend a great deal of what you men by a "variable" and by
"undefined"? In javascript variables are properties of object (and
properties of objects always have values in javascript) and undefined is
a value (the single value of the Undefined type). There is no sense in
which a javascript variable can exist and not have a value, or be
"undefined" before it has a value.

The value of a variable is traditionally undefined before it gets a
value (undefined in the literal semantic sense). I'm not sure how that
works on js compile level, but it is a principle of which you can't
step away:

var x = 'abc'

is one instruction, but is short for

var x
x = 'abc'

First create name x and then give it the value 'abc'. As long as 'abc'
is not assigned, x remains undefined, that is, no value is associated
with the declared name at that point. I try to avoid analogies, but
first you need to have a box before you can put something in it (you
need to have something to put 'abc' in). Before you instruct what's in
the box, nothing is said about what's gonna be in it, but the box must
exist already. At that stage, the contents of the box is (still)
undefined.
(1) reserve the name (declaration) [*]

There is no reserving of names in javascript. The properties of objects
that represent variables are created during "variable instantiation".

Reserving a name is a very simple thought. Every javascript variable
has a name. The programmer reserves a name in order to reference to it
at a later time. The official term is "declaration".
The nature of assigning values in javascript is not certain, nor
important.

Indeed, that is technically not important as long as your scope is to
stay working inside the javascript engine. But the broader concept is
very important in a sense that every computer works that way.
Assigning the undefined value to a property of an object is not distinct
from assigning any other value.


In what sense "free up the memory"? The object that has the property
retains its property, and whatever structure links that property with
its value must continue to exist if the value that is linked to is the
undefined value.

The memory is traditionally freed once the bytes that hold the value
are made available again for other instructions. Say I have 4 bytes to
my disposal on a 7-bit computer, and do

x = 84

then I take one of the 4 bytes and put 0101010 in it, with 1 as closed
circuit ("on") and 0 as broken circuit ("off"). At that point, x holds
a value and I'm using 25% CPU of the 4 available bytes. When I assign
an empty string to x, I just set it to 0000000, but I'm still using 25%
CPU. Then I make x undefined, thus disconnecting the byte from x. Then
I'm again at 0% CPU (4 bytes available again).

I'm not saying this applies to javascript; but it might be worth
testing it. With very large strings, such memory fluctuations might(!)
be measurable in the browser, but I'm afraid of caching mechanisms +
all kinds of intermediate levels.
[...]
Probably I'm thinking in too classic theoretic patterns.

Or maybe you are trying to apply a theory to the wrong structures.

That is possible.
[*] Maybe javascript's "var myvar;" syntax is a far heir of that.

That does not make sense to me.

When you say "var myvar = 'abc'", you pass 2 commands, namely "var
myvar" and "myvar = 'abc'".
 
B

Bart Van der Donck

Richard Cornford schreef:
Which "traditional theory"?

Well, the basics. You once said that you could build your own working
computer. I can't do that because I don't have enough electronical and
technic knowledge for it. But I do know how it should work
conceptually.

What would be your plan to build a computer ?
 
R

Richard Cornford

Bart said:
The value of a variable is traditionally undefined before it gets a
value (undefined in the literal semantic sense).

Again you are being vague about what you mean by a variable. In C, for
example, a subroutine local variable is a fixed size lump of space on
the stack. It is referenced relative to (offset from) the stack pointer
and while a name might stand in for that reference in the source code
that does not mean the concept and mechanism maps well to javascript.

In javascript the variables are named properties of objects, and all
properties of objects have values.
I'm not sure how that works on js compile level, but it is a principle
of which you can't step away:

But is it a principle that applies at the level that javascript
operates at?
var x = 'abc'

is one instruction, but is short for

var x
x = 'abc'

First create name x and then give it the value 'abc'.

In javascript the "first create name x" stage is "first create a
property of the Variable object and assign it the Undefined value".
As long as 'abc' is not assigned, x remains undefined,

In javascript - x - retains its undefined value.
that is, no value is associated with the declared name at that point.

Which is precisely not the case in javascript, where the general fact
that no property of an object can exist without having a value requires
that variables have values from the moment of their creation.
I try to avoid analogies, but first you need to have a box before
you can put something in it (you
need to have something to put 'abc' in).

But simultaneously, as soon as you have a box it has something in it
(if only a vacuum (as the most 'not containing anything' that a
container can achieved)).
Before you instruct what's in the box, nothing is said about what's
gonna be in it, but the box must exist already. At that stage, the
contents of the box is (still) undefined.

Going back to reserving space on the stack, it would be possible for a
complier to do either of, zeroing all the bits in the reserved space or
leaving it as it is. In both cases the 'variable' would have a 'value'
(in some sense) prior to any explicit assignment of a value.
(1) reserve the name (declaration) [*]

There is no reserving of names in javascript. The properties of objects
that represent variables are created during "variable instantiation".

Reserving a name is a very simple thought. Every javascript variable
has a name. The programmer reserves a name in order to reference
to it at a later time. The official term is "declaration".

Maybe, but the mechanism is the creation of a named property of an
object at a particular point in the execution of the code.
Indeed, that is technically not important as long as your scope is to
stay working inside the javascript engine. But the broader concept is
very important in a sense that every computer works that way.

But when you are talking about something that represents a named
property of an object having an internal attribute that links it (in
some way) to a manifestation of a value, and none of: the nature of the
property, the nature of the value, nor the linking between the two, are
specified beyond the behaviour of the resulting system that is to be
achieved, talking of bit patters is meaninglessly specific (even if the
nature of a computer implies that bit patterns must be being set
somewhere).
The memory is traditionally freed once the bytes that hold the value
are made available again for other instructions.

The implication of the way you worded that was that "free up the
memory" referred to the memory for the variable. But as the variable is
a property of an object the memory needed for the variable itself
cannot be freed until the value of the object that holds the property
is freed.
Say I have 4 bytes to
my disposal on a 7-bit computer, and do

x = 84

then I take one of the 4 bytes and put 0101010 in it, with 1 as closed
circuit ("on") and 0 as broken circuit ("off"). At that point, x holds
a value and I'm using 25% CPU of the 4 available bytes. When I assign
an empty string to x, I just set it to 0000000,

I suspected that your concept of an empty string was a NULL (zero byte
in this context) terminated sequence of bytes (or maybe 16 bit words).
In Java, for example, a string is an object and so 'assigning' even an
empty string may mean writing a 32 bit memory address into your 4
bytes.
but I'm still using 25% CPU. Then I make x undefined, thus disconnecting
the byte from x. Then I'm again at 0% CPU (4 bytes available again).

Again you have a concept that is certainly above machine code (where
bytes being 'available' or 'unavailable' is not externally imposed),
but at the same time a concept that is below (or not sufficient to
explain) Java.
I'm not saying this applies to javascript;

There is very little that can usefully be said about javascript that
can be expressed in terms of writing values into specific memory
locations. Javascript is just too abstracted from that level.
but it might be worth testing it.

Testing what precisely?
With very large strings, such memory fluctuations might(!)
be measurable in the browser,

They are.
but I'm afraid of caching mechanisms +
all kinds of intermediate levels.
[...]
Probably I'm thinking in too classic theoretic patterns.

Or maybe you are trying to apply a theory to the wrong structures.

That is possible.
[*] Maybe javascript's "var myvar;" syntax is a far heir of that.

That does not make sense to me.

When you say "var myvar = 'abc'", you pass 2 commands, namely "var
myvar" and "myvar = 'abc'".

I cannot see any way of interpreting your first statement that results
in the second.

Richard.
 

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
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top