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

Cookie problem! Getting error when passing the expire date.

P: n/a
HI! I keep on getting this error and I have tried different things but I am
not sure how to send the expiring date.

The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

----------------------------------------------------
I have this in a .js file and in the head section.

function Set_Cookie(name,value,expires,path,domain,secure) {
document.cookie = name + "=" +escape(value) +
( (expires) ? ";expires=" + expires.toGMTString() : "") +
( (path) ? ";path=" + path : "") +
( (domain) ? ";domain=" + domain : "") +
( (secure) ? ";secure" : "");
}
I am calling it like this.

<script type="text/javascript">
Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC", "/");
</script>

What Am I doing wrong?

Paul
Apr 27 '06 #1
Share this Question
Share on Google+
32 Replies


P: n/a

paul wrote:
The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

( (expires) ? ";expires=" + expires.toGMTString() : "") + [snip] Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC", "/");

What Am I doing wrong?


The toGMTString() method is method belonging to the Date object. In
your example, you are passing a string "Thu, 2 ...etc". Strings don't
have toGMTString() method.

Apr 27 '06 #2

P: n/a

"paul" <pa***********@sympatico.ca> wrote in message
news:1S*******************@news20.bellglobal.com.. .
HI! I keep on getting this error and I have tried different things but I
am not sure how to send the expiring date.

The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

I am calling it like this.

<script type="text/javascript">
Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC", "/");
</script>

What Am I doing wrong?


toGMTString must be called for a Date object not a string literal.
Your parameter should be generated thus:

var now=new Date( new Date().setDate( now.getDate() + 31) ) //31 days

although if the expiry is intended to be after a fixed number of days,
rather
than a specific date, your function would look better accepting an integer
and using it as above to specify the expiry date.

--
S.C.
Apr 27 '06 #3

P: n/a

paul wrote:
The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

( (expires) ? ";expires=" + expires.toGMTString() : "") + [snip] Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC", "/");

What Am I doing wrong?


The toGMTString() method is method belonging to the Date object. In
your example, you are passing a string "Thu, 2 ...etc". Strings don't
have toGMTString() method.

Apr 27 '06 #4

P: n/a
web.dev wrote:
paul wrote:
The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

( (expires) ? ";expires=" + expires.toGMTString() : "") +

[snip]
Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC",
"/");

What Am I doing wrong?


The toGMTString() method is method belonging to the Date object. In
your example, you are passing a string "Thu, 2 ...etc". Strings don't
have toGMTString() method.


HI! thanks for reponding, hmm, how do I translate the date to numeric? do I
do it like 05, 2 06 2001 20:47:11 ? what about the UTC? do you I use a HEX
value?

Paul
Apr 27 '06 #5

P: n/a
Stephen Chalmers wrote:
"paul" <pa***********@sympatico.ca> wrote in message
news:1S*******************@news20.bellglobal.com.. .
HI! I keep on getting this error and I have tried different things but I
am not sure how to send the expiring date.

The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

I am calling it like this.

<script type="text/javascript">
Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC",
"/"); </script>

What Am I doing wrong?


toGMTString must be called for a Date object not a string literal.
Your parameter should be generated thus:

var now=new Date( new Date().setDate( now.getDate() + 31) ) //31 days

although if the expiry is intended to be after a fixed number of days,
rather
than a specific date, your function would look better accepting an integer
and using it as above to specify the expiry date.


HI! Thanks, I will have to take a different aproach then:

Paul
Apr 27 '06 #6

P: n/a
Stephen Chalmers wrote:
"paul" <pa***********@sympatico.ca> wrote in message
news:1S*******************@news20.bellglobal.com.. .
HI! I keep on getting this error and I have tried different things but I
am not sure how to send the expiring date.

The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

I am calling it like this.

<script type="text/javascript">
Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC",
"/"); </script>

What Am I doing wrong?


toGMTString must be called for a Date object not a string literal.
Your parameter should be generated thus:

var now=new Date( new Date().setDate( now.getDate() + 31) ) //31 days

although if the expiry is intended to be after a fixed number of days,
rather
than a specific date, your function would look better accepting an integer
and using it as above to specify the expiry date.


HI! I am tring somthing else and I get the expired date but I am now having
trouble adding the path

function Set_Cookie(cookieName,cookieValue,nDays,cookiePath ) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue) +
";expires="+expire.toGMTString(); + "path="+cookiePath";
}

I am using this to call it.

<script type="text/javascript">
Set_Cookie("checkpopupwarning", "1", "7", "/");
</script>

I am getting the following as an error:

"Error: unterminated string literal"

I have tried different things but I am still getting this error or
"cookiePath not defined".

Am I adding this together correctly?
Paul
Apr 27 '06 #7

P: n/a

paul wrote:
[snip]
";expires="+expire.toGMTString(); + "path="+cookiePath";


First, remove the semicolon from the call to expire.toGMTString()
Second, you have one too many quotes.

So it should be more like the following:

";expires=" + expire.toGMTString() + "path=" + cookiePath;

Apr 28 '06 #8

P: n/a
web.dev wrote:
paul wrote:
[snip]
";expires="+expire.toGMTString(); + "path="+cookiePath";


First, remove the semicolon from the call to expire.toGMTString()
Second, you have one too many quotes.

So it should be more like the following:

";expires=" + expire.toGMTString() + "path=" + cookiePath;


HI! Great it works, one thing that I dont understand though is why the
forward slash does not need quotes, is it not a string value?

Paul
Apr 28 '06 #9

P: n/a
web.dev wrote:
paul wrote:
[snip]
";expires="+expire.toGMTString(); + "path="+cookiePath";
First, remove the semicolon from the call to expire.toGMTString()
Second, you have one too many quotes.


True.
So it should be more like the following:

";expires=" + expire.toGMTString() + "path=" + cookiePath;


It should be more like

"; expires=" + expire.toGMTString() + "; path=" + cookiePath;
PointedEars
--
The German psychs, the German authorities, the German secret service agents
are [...] fanatics, they are insane and known of persecuting innocent people
and Scientologists. -- "The only real Barbara Schwarz", dsw.scientology,
<16**************************@posting.google.com >
Apr 28 '06 #10

P: n/a
JRS: In article <44********@mk-nntp-2.news.uk.tiscali.com>, dated Thu,
27 Apr 2006 22:35:00 remote, seen in news:comp.lang.javascript, Stephen
Chalmers <ig******@lycos.co.uk> posted :
"paul" <pa***********@sympatico.ca> wrote in message
news:1S*******************@news20.bellglobal.com. ..
HI! I keep on getting this error and I have tried different things but I
am not sure how to send the expiring date.

The error that I am getting in Firefox 1.5 is

"Error: expires.toGMTString is not a function"

I am calling it like this.

<script type="text/javascript">
Set_Cookie("checkpopupwarning", "1", "Thu, 2 Aug 2001 20:47:11 UTC", "/");
</script>

What Am I doing wrong?

You are already supplying a string, so toGMTString() is not wanted.

toGMTString must be called for a Date object not a string literal.
Your parameter should be generated thus:

var now=new Date( new Date().setDate( now.getDate() + 31) ) //31 days


Does not work.

now = new Date(+new Date()+31*864e5) // go ahead 31*24 hours

with (now=new Date()) setDate(getDate()+31) // go ahead 31 days

with (now=new Date()) setMonth(getMonth()+1) // go ahead 1 month

with (now=new Date()) setHours(31*24,0,0,0) // start of 31st day

Technically, IIRC, the format specified for cookies is not exactly that
usually given by toGMTString, but it seems not to matter.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 28 '06 #11

P: n/a
JRS: In article <WP*******************@news20.bellglobal.com>, dated
Thu, 27 Apr 2006 18:45:01 remote, seen in news:comp.lang.javascript,
paul <pa***********@sympatico.ca> posted :

function Set_Cookie(cookieName,cookieValue,nDays,cookiePath ) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue) +
";expires="+expire.toGMTString(); + "path="+cookiePath";
}

Don't let your posting agent line-wrap your code; for code legibility, I
suggest a space after each comma, a space each side of a concatenating
+, and indenting by 2 (or 3) spaces per level.

You don't need two Date Objects.

If a Date Object is needed, but it does not need to be set to the
current date/time, use new Date(0) for speed and reproducibility.

ISTM that in such a case it's not necessary to check nDays, which will
almost always be supplied as a Number literal. But if it is checked,
ISTM that it should be more fully checked - NaN, +-Infinity, function,
.... .

Since Cookie setting does not need speed, it might be simplest to
convert it to String, check with RegExp, and if OK convert back with
unary +. Alternatively, X|0 seems to convert most things to a
respectable Number.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 28 '06 #12

P: n/a
Dr John Stockton wrote:
JRS: In article <WP*******************@news20.bellglobal.com>, dated
Thu, 27 Apr 2006 18:45:01 remote, seen in news:comp.lang.javascript,
paul <pa***********@sympatico.ca> posted :

function Set_Cookie(cookieName,cookieValue,nDays,cookiePath ) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
expire.setTime(today.getTime() + 3600000*24*nDays);
document.cookie = cookieName+"="+escape(cookieValue) +
";expires="+expire.toGMTString(); + "path="+cookiePath";
}

Don't let your posting agent line-wrap your code; for code legibility, I
suggest a space after each comma, a space each side of a concatenating
+, and indenting by 2 (or 3) spaces per level.

You don't need two Date Objects.

If a Date Object is needed, but it does not need to be set to the
current date/time, use new Date(0) for speed and reproducibility.

ISTM that in such a case it's not necessary to check nDays, which will
almost always be supplied as a Number literal. But if it is checked,
ISTM that it should be more fully checked - NaN, +-Infinity, function,
... .

Since Cookie setting does not need speed, it might be simplest to
convert it to String, check with RegExp, and if OK convert back with
unary +. Alternatively, X|0 seems to convert most things to a
respectable Number.


HI!, Thanks for the responce, I will check out RegExp and convert it.

Thanks again.

Paul
Apr 29 '06 #13

P: n/a
Dr John Stockton wrote on 28 apr 2006 in comp.lang.javascript:
Alternatively, X|0 seems to convert most things to a
respectable Number.


+X|0

===========

x='a'
y=+x|0
alert(y);

x='5'
y=+x|0
alert(y);

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 29 '06 #14

P: n/a
JRS: In article <Xn*******************@194.109.133.242>, dated Sat, 29
Apr 2006 09:33:41 remote, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> posted :
Dr John Stockton wrote on 28 apr 2006 in comp.lang.javascript:
Alternatively, X|0 seems to convert most things to a
respectable Number.


+X|0

===========

x='a'
y=+x|0
alert(y);

x='5'
y=+x|0
alert(y);


For me, the + makes no visible difference to those.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 29 '06 #15

P: n/a
Dr John Stockton wrote on 29 apr 2006 in comp.lang.javascript:
JRS: In article <Xn*******************@194.109.133.242>, dated Sat, 29
Apr 2006 09:33:41 remote, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> posted :
Dr John Stockton wrote on 28 apr 2006 in comp.lang.javascript:
Alternatively, X|0 seems to convert most things to a
respectable Number.


+X|0

===========

x='a'
y=+x|0
alert(y);

x='5'
y=+x|0
alert(y);


For me, the + makes no visible difference to those.


You are right.

Because a string, like a zero, equals false, when |-ing.

So if you want only NaN to default to 1, you are in trouble anyway:

x='3'
y=x|1
alert(y); // returns 3, correct

x='aString'
y=x|1
alert(y); // returns 1, correct

x='0'
y=x|1
alert(y); // returns 1,
//"should" return 0, x being convertable to a number.

Not much of a zero tolerance!

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 30 '06 #16

P: n/a
"Evertjan." <ex**************@interxnl.net> writes:
You are right.

Because a string, like a zero, equals false, when |-ing.
More precisely, since "|" is a number operator, not a logical
operator, a non-numeral string converts to NaN.

(For logical comparison, i.e., conversion to boolean, only
the empty string becomes false).

There are a lot of implicit type conversions in
x | 1
where "x" holds a string. More explicitly (using the internal names
for conversion and showing only the ones that makes a difference) it
corresponds to:

toInt32(toNumber(x)) | toInt32(1)

If the string in "x" is not a numeral (as per the definition of
"toNumber"), "toNumber(x)" yields "NaN". Then "toInt32(NaN)"
yields "+0" and "0 | 1" gives 1.

x='0'
y=x|1
alert(y); // returns 1,
//"should" return 0, x being convertable to a number.


And
x='24'
y = x|1;
alert(y); // alerts 25

"Bitwise or" is not a logical guard in the way "logical or" is.
This zero-problem would still be there for || though:
Number("0") || 1
yields 1 as well, since 0 converts to false.

It really should be something like:

function toNumberWithDefault(x,def) {
var n = Number(x);
return isNaN(n) ? def : n;
}

/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.'
Apr 30 '06 #17

P: n/a
Lasse Reichstein Nielsen wrote on 30 apr 2006 in comp.lang.javascript:
x='0'
y=x|1
alert(y); // returns 1,
//"should" return 0, x being convertable to a number.
And
x='24'
y = x|1;
alert(y); // alerts 25


Wow! Yes.
"Bitwise or" is not a logical guard in the way "logical or" is.
This zero-problem would still be there for || though:
Number("0") || 1
yields 1 as well, since 0 converts to false.

It really should be something like:

function toNumberWithDefault(x,def) {
var n = Number(x);
return isNaN(n) ? def : n;
}

alert(toNumberWithDefault('',7777)) // alerts 0

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 30 '06 #18

P: n/a
"Evertjan." <ex**************@interxnl.net> writes:
Lasse Reichstein Nielsen wrote on 30 apr 2006 in comp.lang.javascript:

function toNumberWithDefault(x,def) {
var n = Number(x);
return isNaN(n) ? def : n;
}

alert(toNumberWithDefault('',7777)) // alerts 0


I guess it's a matter of definition whether "" represents a number
(that would be 0 then) or not. The "Number" function appears to think
it does.

If one wants a clearly defined set of number strings to be converted
to numbers, then the format of the string should be tested before
using any standard conversion function, since all standard functions
seem to have the odd case :)

/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.'
Apr 30 '06 #19

P: n/a
VK

Lasse Reichstein Nielsen wrote:
I guess it's a matter of definition whether "" represents a number
(that would be 0 then) or not. The "Number" function appears to think
it does.


Not the Number only, but the whole language itself, welcome to the
JavaScript world. Are we aware of runtime typezation? ;-)

alert("" == 0); // true

btw and AFAIK Number is supposed to be used as constructor, not as a
method.

Apr 30 '06 #20

P: n/a
VK wrote:
Lasse Reichstein Nielsen wrote:
I guess it's a matter of definition whether "" represents
a number (that would be 0 then) or not. The "Number" function
appears to think it does.
Not the Number only, but the whole language itself, welcome
to the JavaScript world.


Yet another example of your stringing words together and producing a
result that says nothing.
Are we aware of runtime typezation? ;-)
Are 'we' making up our own terminology agian?
alert("" == 0); // true
That is the specified (and so expected) result. The type-converting
comparison will tend to do numeric comparisons so (as Lasse stated) the
fact that the empty string type-converts to numeric zero makes this
comparison true.
btw and AFAIK Number is supposed to be used as constructor,
not as a method.


One of the main things you would benefit from learning is an
appreciation of how very little you actually do know. See ECMA 262, 3rd
Ed. Section 15.7.1 "The Number Constructor Called as a Function", and
appreciate that a function should not be spoken of as a 'method' unless
it is called as a member of an object.

Richard.
Apr 30 '06 #21

P: n/a
On 30/04/2006 20:51, VK wrote:
Lasse Reichstein Nielsen wrote:
I guess it's a matter of definition whether "" represents a number
(that would be 0 then) or not. The "Number" function appears to
think it does.
Not the Number only, but the whole language itself, welcome to the
JavaScript world. Are we aware of runtime typezation? ;-)


Do you mean implicit type conversion, or the status of ECMAScript as a
dynamically-typed language?
alert("" == 0); // true
The above is a product of the former.

When strings are compared to numbers, the string is type-converted to a
number. In fact, type conversion, in general, is typically biased
towards numbers. Probably because, as far as comparison is concerned, a
number is the most simple and efficient.
btw and AFAIK Number is supposed to be used as constructor, not as a
method.


Not really. All of the constructor functions that are analogous to
primitive types (String, Boolean, and Number), and the Object
constructor function, perform type conversion when called as functions.
The other constructor functions typically act as though they were called
with use of the new operator (though Date is an odd exception).

The Number and Boolean constructor functions are typically less
efficient than the equivalent operator sequences (unary plus [+] and
logical NOT [!], respectively).

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Apr 30 '06 #22

P: n/a
Richard Cornford wrote:
VK wrote:
btw and AFAIK Number is supposed to be used as constructor,
not as a method.


One of the main things you would benefit from learning is an
appreciation of how very little you actually do know. See ECMA 262, 3rd
Ed. Section 15.7.1 "The Number Constructor Called as a Function", and
appreciate that a function should not be spoken of as a 'method' unless
it is called as a member of an object.


As for the former, I concur. As for the latter, one should keep in mind
that every function is indeed a method (ES3 Final, 4.3.3) in ECMAScript
implementations -- if its owner is (and therefore the function is called as
a member of) only the Variable Object of the execution context (the Global
Object for globally declared functions, and built-in top-level functions
such as Number(); see ES3 Final, subsection 10.1.5). However, to
distinguish between constructors and methods is typically VK again; every
constructor is a function, hence it is also a method.
Regards,
PointedEars
--
There are two possibilities: Either we are alone in the
universe or we are not. Both are equally terrifying.
-- Arthur C. Clarke
Apr 30 '06 #23

P: n/a
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
As for the latter, one should keep in mind that every function is
indeed a method (ES3 Final, 4.3.3) in ECMAScript implementations --
if its owner is (and therefore the function is called as a member
of) only the Variable Object of the execution context (the Global
Object for globally declared functions, and built-in top-level
functions such as Number(); see ES3 Final, subsection 10.1.5).


Not all functions are methods, although there isn't really a lot
you can do about function without them becoming methods (which
also proves that the official terminology is not very good, if
all functions are also methods, we don't need two words).

Witnes:
var x = ((function(){}).prototype={}),42);

The function created here has no other object property referring to it
than it's own prototype.constructor, so when we override this, it's no
longer a method. We also have no reference to it, since all variables
are also properties of objects (global, arguments or activation
object), and we dare not call it, or it will become the method
arguments.callee, so it's completely useless :)
A more relevant distinction would be whether a function is *called* as
a method or not (i.e., called through a Reference value, setting
the value of the "this" operator in the method body during the call,
instead of it defaulting to the global object).

/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.'
Apr 30 '06 #24

P: n/a
Michael Winter <m.******@blueyonder.co.uk> writes:
The Number and Boolean constructor functions are typically less
efficient than the equivalent operator sequences (unary plus [+] and
logical NOT [!], respectively).


(that would be double-not [!!])
The performance might be very slightly better for operators that doesn't
do anything except type convert their arguments, than for function calls
that then do the same.

On the other hand, a large number of type conversions inside a tight
loop does sound like something that should be recoded, and outside
of such a loop, the performance difference is too small to measure.

I personally find the conversion functions easier to read than the
operators, which is another measure to choose by :)

/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.'
Apr 30 '06 #25

P: n/a
Thomas 'PointedEars' Lahn wrote:
Richard Cornford wrote:
VK wrote:
btw and AFAIK Number is supposed to be used as constructor,
not as a method.
One of the main things you would benefit from learning is an
appreciation of how very little you actually do know. See
ECMA 262, 3rd Ed. Section 15.7.1 "The Number Constructor
Called as a Function", and appreciate that a function should
not be spoken of as a 'method' unless it is called as a member
of an object.


As for the former, I concur. As for the latter, one should
keep in mind that every function is indeed a method (ES3
Final, 4.3.3) in ECMAScript implementations -


Every function?

(function(){;})();

- a function that is never assigned as a property of any object.
- if its owner is (and therefore the function is called as a
member of) only the Variable Object of the execution context
(the Global Object for globally declared functions, and
built-in top-level functions such as Number(); see ES3 Final,
subsection 10.1.5).
So is that supposed to paraphrase as: every function is a method in
ECMAScript implementations - if its owner is only the Variable Object of
the execution context? Because that does not rally make sense.
However, to distinguish between constructors and methods is
typically VK again; every constructor is a function, hence
it is also a method.


No, the 'every function is a method' part of that is not true.

However, taking an overly tolerant attitude towards the meaning of
'method', where any function that is in some sense a property of an
(any) object just acts to render the term redundant, as we can say
'function' in its place and be expressing no less.

'Method' really needs to be used and understood in its object-oriented
sense, where a method is a member of an object. However, in javascript a
function being a member of an object is not sufficient; the function's
relationship with the object is not significantly influenced by its
being just a member of the object, it is how the function is called that
gives a function the special relationship with the object to the extent
that it would be meaningful to call it a 'method'. In short the
categorisation 'method' only meaningfully applies to functions in which
the - this - keyword can be used to refer to the object on which the
function is a member.

This means that the telling discriminator between a function and a
method should be in section 11.2.3; and only circumstances where the -
MemberExpression - resulted in a Reference type that had a non-null base
object that is not an activation object (the global object is never an
activation object), and so will result in a non-null value being passed
into the [[Call]] as the - this - value, would justify labelling a
function a 'method'. For practical purposes the sub-category of
functions that could be labelled 'methods' would be those intended to be
called in this way, otherwise the distinction is meaningless and the
term 'method' is redundant.

Richard.
Apr 30 '06 #26

P: n/a
Richard Cornford wrote:
Thomas 'PointedEars' Lahn wrote:
Richard Cornford wrote:
VK wrote:
btw and AFAIK Number is supposed to be used as constructor,
not as a method.
One of the main things you would benefit from learning is an
appreciation of how very little you actually do know. See
ECMA 262, 3rd Ed. Section 15.7.1 "The Number Constructor
Called as a Function", and appreciate that a function should
not be spoken of as a 'method' unless it is called as a member
of an object.
As for the former, I concur. As for the latter, one should
keep in mind that every function is indeed a method (ES3
Final, 4.3.3) in ECMAScript implementations -


Every function?

(function(){;})();

- a function that is never assigned as a property of any object.


True, I misread section 13.2 here.
- if its owner is (and therefore the function is called as a
member of) only the Variable Object of the execution context
(the Global Object for globally declared functions, and
built-in top-level functions such as Number(); see ES3 Final,
subsection 10.1.5).


So is that supposed to paraphrase as: every function is a method in
ECMAScript implementations - if its owner is only the Variable Object of
the execution context? Because that does not rally make sense.


I meant to say that the fact that a function can be referred to as a
property of the Variable Object of an execution context is already
sufficient for it to become a method. There is no explicit mentioning
of the owner object required in the reference.
However, taking an overly tolerant attitude towards the meaning of
'method', where any function that is in some sense a property of an
(any) object just acts to render the term redundant, as we can say
'function' in its place and be expressing no less.


You have disproven that yourself already. Standalone FunctionExpressions
create Function( object)s that are functions, but not methods, as they have
no defined owner object.
PointedEars
--
Alcohol and Math don't mix. So please don't drink and derive!
May 2 '06 #27

P: n/a
Richard Cornford said on 01/05/2006 8:17 AM AEST:
[...]
This means that the telling discriminator between a function and a
method should be in section 11.2.3; and only circumstances where the -
MemberExpression - resulted in a Reference type that had a non-null base
object that is not an activation object (the global object is never an
activation object)
Does that mean that there is no activation object associated with the
global object? That is, only function code creates an activation
object? That appears to be inferred by 10.1.6:

"When control enters an execution context for function code, an
object called the activation object is created and associated
with the execution context."

Similarly for the arguments object. There seems to be a clear
difference to constructors and no mention specifically of the global object.
, and so will result in a non-null value being passed into the [[Call]] as the - this - value, would justify labelling a
function a 'method'. For practical purposes the sub-category of
functions that could be labelled 'methods' would be those intended to be
called in this way, otherwise the distinction is meaningless and the
term 'method' is redundant.


I paraphrased this as 'wherever an activation object has a -this- value
of an object other than the global object, the function is a method'.

Is that correct?
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
May 2 '06 #28

P: n/a
VK

Richard Cornford wrote:
alert("" == 0); // true
That is the specified (and so expected) result. The type-converting
comparison will tend to do numeric comparisons so (as Lasse stated) the
fact that the empty string type-converts to numeric zero makes this
comparison true.


Right. And this is whay I posted it in reference to the OP's evident
surprise that in JavaScript empty string may result in 0, 0 in empty
string and both to false.

Thank you for your explanations, but actually I did not need them :-)

One of the main things you would benefit from learning is an
appreciation of how very little you actually do know. See ECMA 262, 3rd
Ed. Section 15.7.1 "The Number Constructor Called as a Function", and
appreciate that a function should not be spoken of as a 'method' unless
it is called as a member of an object.


You may benefit from reading specs once over again :-) Every
conventional JavaScript function (thus pre-defined by the engine or
defined on the top level in <script> block) is a method of the Global
object. The fact that you don't have to type this.myFunction() or
window.myFunction() in the global context is just a convenience
shortcut, same way as say in Perl $foo = 'bar'; actually means
main::$foo = 'bar';

JavaScript doesn't have at all "functions" in the sense on QBasic or
other pre-OOP languages. Yet for the simplicity it is normal to call
functions declared in the global context and not appertaining to any
explicit object as "functions", not as "methods of Global object".

On the conventional level of JavaScript programming it is all to say I
guess. On a higher level (XBL/ IE Behaviors) you can have N amount of
Global objects created for the given document. The first one is the
regular Global from <script>, others are created for each declared
behavior type and not directly accessible from the <script> Global.
Respectively their functions are members of the relevant Global, but
not of the <script> Global.

But ECMA Books do not go so far, and it must be out of your interests.

May 2 '06 #29

P: n/a
RobG wrote:
Richard Cornford said on 01/05/2006 8:17 AM AEST:
[...]
This means that the telling discriminator between a function
and a method should be in section 11.2.3; and only
circumstances where the - MemberExpression - resulted in
a Reference type that had a non-null base object that is
not an activation object (the global object is never an
activation object)
Does that mean that there is no activation object associated
with the global object? That is, only function code creates
an activation object? That appears to be inferred by 10.1.6:

"When control enters an execution context for function code,
an object called the activation object is created and
associated with the execution context."

Similarly for the arguments object.


Yes, the global execution context doesn't need an Activation object, and
it has no arguments so it doesn't need and arguments object either.
There seems to be a clear difference to constructors and
no mention specifically of the global object.
I am not sure what you are getting at there.
, and so will result in a non-null value being passed
into the [[Call]] as the - this - value, would justify
labelling a function a 'method'. For practical purposes
the sub-category of functions that could be labelled
'methods' would be those intended to be called in this
way, otherwise the distinction is meaningless and the
term 'method' is redundant.


I paraphrased this as 'wherever an activation object has
a -this- value of an object other than the global object,
the function is a method'.

Is that correct?


Pretty much. I would accept that there is a grey area when the - this -
value passed into a function call is the global object, rather than
being null. Something like - window.globalFunction()- - is a method call
but the - this - value will be the global object, and so the result will
be indistinguishable from - (x = globalFunction)(); - (apart from the
side-effect assignment).

Richard.
May 3 '06 #30

P: n/a
Thomas 'PointedEars' Lahn wrote:
Richard Cornford wrote:
Thomas 'PointedEars' Lahn wrote:
Richard Cornford wrote: <snip> ... , and appreciate that a function should not be spoken
of as a 'method' unless it is called as a member of an
object.

As for the former, I concur. As for the latter, one should
keep in mind that every function is indeed a method (ES3
Final, 4.3.3) in ECMAScript implementations -
Every function?

(function(){;})();

- a function that is never assigned as a property of any object.


True, I misread section 13.2 here.
- if its owner is (and therefore the function is called as a
member of) only the Variable Object of the execution context
(the Global Object for globally declared functions, and
built-in top-level functions such as Number(); see ES3 Final,
subsection 10.1.5).


So is that supposed to paraphrase as: every function is a method
in ECMAScript implementations - if its owner is only the Variable
Object of the execution context? Because that does not rally make
sense.


I meant to say that the fact that a function can be referred to
as a property of the Variable Object of an execution context is
already sufficient for it to become a method. There is no
explicit mentioning of the owner object required in the reference.


But when an unqualified Identifier is used to reference a function that
is a property of a Variable object that object is still the base
property of the resulting Reference type. That object does not get
passed into the function call as the - this - value because a Variable
object is also its Activation object (if it was created for a function's
execution context)) so step 7 in the algorithm for section 11.2.3
substitutes null as the - this - value.
However, taking an overly tolerant attitude towards the meaning of
'method', where any function that is in some sense a property of an
(any) object just acts to render the term redundant, as we can say
'function' in its place and be expressing no less.


You have disproven that yourself already.


Shown that there is a distinction maybe.
Standalone FunctionExpressions create Function( object)s that are
functions, but not methods, as they have no defined owner object.


Well, I had not taken into account the fact that Lasse pointed out; that
even the result of an anonymous function expression will be a property
of its own prototype, so all functions are properties of some object
when created. Though Lasse also pointed out that they do not need to
remain so, so it remains possible for a function not to be a property of
any object (if often less than useful when it is). So it would be
possible to use 'method' as opposed to 'function' to express a tangible
difference, but such a minor one that it seems ridiculous to squander a
term as loaded and expressive as 'method' in order to draw such a minor
distinction.

I am also reminded that all functions have a [[Construct]] method, and
so it would be as reasonable to label all functions 'constructors', at
which point we have three terms that could be used to express
significant aspects of the use of functions being reduced the virtually
synonymous terms for functions, just because an overly pedantic attitude
could justify that application of the terms.

I will stand by my original statement; that functions should not be
labelled methods unless they are _called_ as properties of objects. We
should consistently use the diversity of terminology to convey useful
concepts.

Richard.
May 3 '06 #31

P: n/a
VK wrote:
<snip>
... .The fact that you don't have to type this.myFunction() or
window.myFunction() in the global context is just a convenience
shortcut, same way as say in Perl $foo = 'bar'; actually means
main::$foo = 'bar';
Bullshit. It is the fact that the Reference type that results form the
evaluation of the property accessors - this.myFunction - and -
window.myFunction - is indistinguishable from the Reference type that
results from the scope chain resolution of - myFunction - (when -
myFunction - is defined in the global execution context, and not defined
in a intervening scope) that allows the latter to be used in the pace of
the former. There is no "convenience shortcut" here, just alternative
ways of achieving he same outcome.
JavaScript doesn't have at all "functions" in the sense on
QBasic or other pre-OOP languages.
QBasic is not a pre OOP language, it is quite a recent language.
Yet for the simplicity it is normal to call functions declared
in the global context and not appertaining to any explicit
object as "functions", not as "methods of Global object".
It is normal to call any and all instances of javascript's function
objects a "function".
On the conventional level of JavaScript programming it is
all to say I guess.
That does not make sense.
On a higher level (XBL/ IE Behaviors) ... <snip>

Irrelevant to your inappropriate (and so potentially misleading) use of
terminology.

<snip> But ECMA Books do not go so far, and it must be out of
your interests.


That isn't quite a sentence either.

Richard.
May 3 '06 #32

P: n/a
Richard Cornford wrote:
Thomas 'PointedEars' Lahn wrote:
I meant to say that the fact that a function can be referred to
as a property of the Variable Object of an execution context is
already sufficient for it to become a method. There is no
explicit mentioning of the owner object required in the reference.
But when an unqualified Identifier is used to reference a function that
is a property of a Variable object that object is still the base
property of the resulting Reference type. That object does not get
passed into the function call as the - this - value because a Variable
object is also its Activation object (if it was created for a function's
execution context)) so step 7 in the algorithm for section 11.2.3
substitutes null as the - this - value.


True. However, section 10.2.3 (Function Code) states:

| [...]
| * The caller provides the `this' value. If the `this' value provided by
| the caller is not an object (including the case where it is `null'),
| then the `this' value is the global object.
I am also reminded that all functions have a [[Construct]] method, and
so it would be as reasonable to label all functions 'constructors',


I don't think so. The reason why I think it is acceptable to call all
functions methods is that they are actually [[Call]]ed. Therefore, I
find it acceptable to call those methods/functions constructors that are
actually [[Construct]]ed, or are intended to be [[Construct]] by the
developer; not all functions/methods.
PointedEars
--
But he had not that supreme gift of the artist, the knowledge of
when to stop.
-- Sherlock Holmes in Sir Arthur Conan Doyle's
"The Adventure of the Norwood Builder"
May 11 '06 #33

This discussion thread is closed

Replies have been disabled for this discussion.