Updated FAQ

G

Garrett Smith

How do I trim whitespace?
Change code to coerce the thisArg to String, as per ES5
- Bozhilov

How do I POST a form to a new window?
Change example to use method="post"
-Saarikumpu

How do I disable the right mouse button?
Fix the grammar
- Stockton

How can I see in javascript if a web browser accepts cookies?
Replace links to cookiecentral and galasoft with MDC link
- Lahn

If I missed something, say something.

Thanks.
 
A

Asen Bozhilov

Garrett said:
How do I trim whitespace?
   Change code to coerce the thisArg to String, as per ES5

| return (this + "").replace(/^\s+|\s+$/g, '');

Should be:
("" + this)

See in JScript:

var o = {toString : null, valueOf : null};
window.alert('' + o); //empty string
window.alert(o + ''); //[object]

var xhr = new ActiveXObject('Microsoft.XMLHTTP');
window.alert('' + xhr); //empty string
window.alert(xhr + ''); //undefined

In your thread about "Code Guidelines" i payed attention about that:
<URL: http://groups.google.bg/group/comp.lang.javascript/msg/1528f612e31f09fe>

Happy New Year!
 
D

David Mark

How do I trim whitespace?
   Change code to coerce the thisArg to String, as per ES5
  - Bozhilov

How do I POST a form to a new window?
   Change example to use method="post"
  -Saarikumpu

How do I disable the right mouse button?
   Fix the grammar
   - Stockton

How can I see in javascript if a web browser accepts cookies?
   Replace links to cookiecentral and galasoft with MDC link
   - Lahn

If I missed something, say something.

Thanks.

The Contributors section in the Notes is years out of date.
 
G

Garrett Smith

Asen said:
Garrett said:
How do I trim whitespace?
Change code to coerce the thisArg to String, as per ES5

| return (this + "").replace(/^\s+|\s+$/g, '');

Should be:
("" + this)

See in JScript:

var o = {toString : null, valueOf : null};
window.alert('' + o); //empty string
window.alert(o + ''); //[object]

var xhr = new ActiveXObject('Microsoft.XMLHTTP');
window.alert('' + xhr); //empty string
window.alert(xhr + ''); //undefined

In your thread about "Code Guidelines" i payed attention about that:
<URL: http://groups.google.bg/group/comp.lang.javascript/msg/1528f612e31f09fe>

Right, I see that now. Weird! It seems that with Host object in IE, the
+ operator behaves differently.

I've changed the example to:-
| if(!String.prototype.trim) {
| String.prototype.trim = function() {
| // XXX IE ("" + this) and not (this + "") for Host obj
|
//http://groups.google.com/group/comp.lang.javascript/msg/fe387847c719119a
| return ("" + this).replace(/^\s+|\s+$/g, '');
| };
| }

Look OK?
 
A

Asen Bozhilov

Garrett said:
Right, I see that now. Weird! It seems that with Host object in IE, the
+ operator behaves differently.

Yes, but addition operator have different behavior not only with host
objects. See below with native object:

var o = {toString : null, valueOf : null};
try {
window.alert(String(o));
}catch(e) {
window.alert(e instanceof TypeError); //[[DefaultValue]] throw
TypeError
}

window.alert('' + o); //empty primitive string value
window.alert(o + ''); //[object]

They have much more complex algorithm, from presented in ECMA262-3
documentation. And they, definitely doesn't follow specification in
that point.
 
T

Thomas 'PointedEars' Lahn

Garrett said:
Right, I see that now. Weird! It seems that with Host object in IE, the
+ operator behaves differently.

Yet another bug triggered by unwise programming.
I've changed the example to:-
| if(!String.prototype.trim) {

Should have a space after `if'; it is not a function call.
| String.prototype.trim = function() {

See, if you used a method to do this instead you would not be in such a
need of writing identifiers twice. That is one advantage.
| // XXX IE ("" + this) and not (this + "") for Host obj
^^^^
Should be lowercase (that error goes like a red thread through all your
postings; perhaps you have not understood the concept behind it at all?),
and "object" should not be abbreviated like this.

Should be a multi-line documentation comment using a proper task tag:

/* NOTE: JScript requires ("" + this) for host objects */

(But this would be superfluous without implicit typecasting anyway.)
|
//http://groups.google.com/group/comp.lang.javascript/msg/fe387847c719119a
| return ("" + this).replace(/^\s+|\s+$/g, '');
| };
| }

Look OK?

No. There is also no need of or advantage in relying on implicit
typecasting.


PointedEars
 
A

Asen Bozhilov

Thomas said:
Yet another bug triggered by unwise programming.

No. That is yet another bug from Microsoft ECMA-262 implementation,
called with name JScript. In normal case:

new ActiveXObject('Microsoft.XMLHTTP') + ''; //expected TypeError

And nobody suggest to do it that.
No.  There is also no need of or advantage in relying on implicit
typecasting.

| ECMA 5 15.5.4.20
| NOTE The trim function is intentionally generic;
| it does not require that its this value be a
| String object.
| Therefore, it can be transferred
| to other kinds of objects for use as a method.
 
G

Garrett Smith

Asen said:
Yes, but addition operator have different behavior not only with host
objects. See below with native object:

var o = {toString : null, valueOf : null};
That would be expected to throw an error, although it does explain
the problem with Host object. THe problem is that the error occurs in
calling toString and valueOf throws.

ActiveXObject has neither toString nor value of and so String
concatenation throws there.

window.external.toString; // undefined

Comment changed to:-
// XXX JScript ("" + this), not (this + "") for objects w/o toString.
//http://groups.google.com/group/comp.lang.javascript/msg/fe387847c719119a
 
T

Thomas 'PointedEars' Lahn

Garrett said:
That would be expected to throw an error, although it does explain
the problem with Host object.

It does not; check your assumptions. And there is that ominous "Host
object" again -- when will you get it?
THe problem is that the error occurs in
calling toString and valueOf throws.

Parse error.
ActiveXObject has neither toString nor value of and so String

Has what?
concatenation throws there.

Throws what? And no, that is _not_ necessarily the reason why.
window.external.toString; // undefined

Comment changed to:-
// XXX JScript ("" + this), not (this + "") for objects w/o toString.
//http://groups.google.com/group/comp.lang.javascript/msg/fe387847c719119a

Neither writing nor reading appear to be the forté of the current FAQ
maintainer -- goodnight, cljs.


PointedEars
 
A

Asen Bozhilov

Thomas said:
Neither writing nor reading appear to be the forté of the current FAQ
maintainer -- goodnight, cljs.

So what? I told you before. If you have any problem in real life, go
outside and take a fresh air, before start blaming everyone in
c.l.js.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
september.org>, Sun, 3 Jan 2010 23:14:14, Garrett Smith
Right, I see that now. Weird! It seems that with Host object in IE, the
+ operator behaves differently.

I've changed the example to:-
| if(!String.prototype.trim) {
| String.prototype.trim = function() {
| // XXX IE ("" + this) and not (this + "") for Host obj
| //http://groups.google.com/group/comp.lang.javascript/msg/fe387847c71
9119a
| return ("" + this).replace(/^\s+|\s+$/g, '');
| };
| }

Look OK?

No. It is customary to convert to String with +"" and not ""+ - which
means that code correctly following the FAQ and working in 'all'
browsers may later get 'tidied' into the usual form and then work in
'all' browsers but one. If it works properly in at least as many
browsers, I suggest
return String(this).replace(/^\s+|\s+$/g, "");
which is only one character longer.


==== On the Web,

function countdown(){
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getUTCMonth()
var todayd=today.getUTCDate()
...

still has not been corrected. But there are obvious sibling instances
of function countdown() which lack the prime fault, so it probably was
not in the original. Another part of the file has a brace of 1800-year
errors.
 
J

JR

Asen said:
Garrett Smith  wrote:
| return (this + "").replace(/^\s+|\s+$/g, '');
Should be:
("" + this)
See in JScript:
var o = {toString : null, valueOf : null};
window.alert('' + o); //empty string
window.alert(o + ''); //[object]
var xhr = new ActiveXObject('Microsoft.XMLHTTP');
window.alert('' + xhr); //empty string
window.alert(xhr + ''); //undefined
In your thread about "Code Guidelines" i payed attention about that:
<URL:http://groups.google.bg/group/comp.lang.javascript/msg/1528f612e31f09fe>

Right, I see that now. Weird! It seems that with Host object in IE, the
+ operator behaves differently.

I've changed the example to:-
| if(!String.prototype.trim) {
|   String.prototype.trim = function() {
|     // XXX IE ("" + this) and not (this + "") for Host obj
|
//http://groups.google.com/group/comp.lang.javascript/msg/fe387847c719119a
|     return ("" + this).replace(/^\s+|\s+$/g, '');
|   };
| }

Look OK?

It looks completely odd to me. If you test for String.prototype.trim,
then there's no sense in coercing the argument to string, as in (this
+ '') or ('' + this), because the trim method should only be used with
strings. The trim() method proposed in the FAQ looks like a mutant,
and I doubt that the FF native code does such a coercion.

I think that the developer should test whether the argument is a
string before trying to use this method, I mean that kind of check
should be done outside the trim method.

By the way, there's an interesting article about this subject at:
http://blog.stevenlevithan.com/archives/faster-trim-javascript

A very fast regex was proposed there (trim1)

return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');

The reasons are explained in the article.

Cheers,
JR
 
G

Garrett Smith

JR said:
[...]

It looks completely odd to me. If you test for String.prototype.trim,
then there's no sense in coercing the argument to string, as in (this
+ '') or ('' + this), because the trim method should only be used with
strings. The trim() method proposed in the FAQ looks like a mutant,
and I doubt that the FF native code does such a coercion.

You should check those doubts against the specification.

Asen was correct in bringing this up and in the workaround for the
JScript bug he mentioned.
 

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

Similar Threads

Updated FAQ 8
Updated FAQ (2010-04-01) 50
Update to FAQ 1
Changes to FAQ 8
FAQ Proposed Anchor Names 5
FAQ 16
My Library TaskSpeed tests updated 31
FAQ update and proposed updates 2

Members online

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,080
Latest member
mikkipirss

Latest Threads

Top