From: Thomas 'PointedEars' Lahn on
Garrett Smith wrote:

> Thomas 'PointedEars' Lahn wrote:
>> return String(this).replace(/^\s+|\s+$/g, "");
>>
>> should be used instead (ES3F/ES5, 15.5.1), whereas the explicit typecast
>> is only necessary anyway if this method should be callable for non
>> String objects.
>
> The use of String constructor called as a function can result in Error
> when the this value is a host object.

Host objects SHOULD NOT be augmented with properties, or be used as first
argument to Function.prototype.call() or Function.prototype.apply(). Your
argument is not logical.

> String(new ActiveXObject('Microsoft.XMLHTTP'));// IE Error.

True.

> IE throws an Error that, when accessing its `name` property, throws the
> error "Bad variable type".

I wonder where you got this `name' property talk from, though, and what it
would have to do with this code.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: Garrett Smith on
Thomas 'PointedEars' Lahn wrote:
> Garrett Smith wrote:
>
>> Thomas 'PointedEars' Lahn wrote:
>>> return String(this).replace(/^\s+|\s+$/g, "");
>>>
>>> should be used instead (ES3F/ES5, 15.5.1), whereas the explicit typecast
>>> is only necessary anyway if this method should be callable for non
>>> String objects.
>> The use of String constructor called as a function can result in Error
>> when the this value is a host object.
>
> Host objects SHOULD NOT be augmented with properties, or be used as first
> argument to Function.prototype.call() or Function.prototype.apply(). Your
> argument is not logical.
>
>> String(new ActiveXObject('Microsoft.XMLHTTP'));// IE Error.
>
> True.
>
>> IE throws an Error that, when accessing its `name` property, throws the
>> error "Bad variable type".
>
> I wonder where you got this `name' property talk from, though, and what it
> would have to do with this code.
>
An Error has a `name` property. In IE 7 and below, the above snippet
throws an error that, when accessing its `name` property, results in an
error with the message: "Bad variable type".

try {
String(new ActiveXObject('Microsoft.XMLHTTP'));
} catch(ex) {
alert(ex.name); // IE Error: "Bad variable type"
}

Because of the error with the String constructor, `("" + this)` is safer
than String(this).

Additionally, order matters. In IE7 and below, `(this + "")` can result
in a undefined value.

var obj = new ActiveXObject('Microsoft.XMLHTTP');
alert( obj + "" ); // undefined.
alert( "" + obj ); // empty string.
alert( String(obj) ); // Error.

The Error is unacceptable. That leaves two options. The more sensible
output is the empty string in this case.

Also Liveconnect objects can throw such errors:

java.lang.Object + "";


--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Thomas 'PointedEars' Lahn on
Garrett Smith wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Garrett Smith wrote:
>>> Thomas 'PointedEars' Lahn wrote:
>>>> return String(this).replace(/^\s+|\s+$/g, "");
>>>>
>>>> should be used instead (ES3F/ES5, 15.5.1), whereas the explicit
>>>> typecast is only necessary anyway if this method should be callable
>>>> for non String objects.
>>> [...]
>>> IE throws an Error that, when accessing its `name` property, throws the
>>> error "Bad variable type".
>>
>> I wonder where you got this `name' property talk from, though, and what
>> it would have to do with this code.
>
> An Error has a `name` property. In IE 7 and below, the above snippet
> throws an error that, when accessing its `name` property, results in an
> error with the message: "Bad variable type".

That is not what you said before. Learn to post.

> try {
> String(new ActiveXObject('Microsoft.XMLHTTP'));
> } catch(ex) {
> alert(ex.name); // IE Error: "Bad variable type"
> }
>
> Because of the error with the String constructor, `("" + this)` is safer
> than String(this).

Nonsense.

> Additionally, order matters. In IE7 and below, `(this + "")` can result
> in a undefined value.
>
> var obj = new ActiveXObject('Microsoft.XMLHTTP');
> alert( obj + "" ); // undefined.
> alert( "" + obj ); // empty string.
> alert( String(obj) ); // Error.
>
> The Error is unacceptable. That leaves two options. The more sensible
> output is the empty string in this case.

The more sensible approach is not to do that in the first place.
Which part of

>> Host objects SHOULD NOT be augmented with properties, or be used as
>> first argument to Function.prototype.call() or
>> Function.prototype.apply(). Your argument is not logical.

did you not get?

> Also Liveconnect objects can throw such errors:
>
> java.lang.Object + "";

See above.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
From: Garrett Smith on
Thomas 'PointedEars' Lahn wrote:
> Garrett Smith wrote:
>
>> Thomas 'PointedEars' Lahn wrote:
>>> Garrett Smith wrote:
>>>> Thomas 'PointedEars' Lahn wrote:
>>>>> return String(this).replace(/^\s+|\s+$/g, "");
>>>>>

[...]

>>
>> The Error is unacceptable. That leaves two options. The more sensible
>> output is the empty string in this case.
>
> The more sensible approach is not to do that in the first place.
> Which part of
>

Try to differentiate between what the caller should not be doing vs how
the function handles the caller doing that.

>>> Host objects SHOULD NOT be augmented with properties, or be used as
>>> first argument to Function.prototype.call() or
>>> Function.prototype.apply(). Your argument is not logical.
>
> did you not get?
>

I understand and agree. However, it is reasonable to allow for an object
to be the first argument to `trim`.

Where the `this` argument to `trim` is a host object, ideally, the
result should be consistent. It is less than ideal to inconsistently
throw errors, depending on the implementation (browser).

The conclusions are: (1) It is good to be consistent in the return type
and (2) bad to throw errors inconsistently.

The ideal solution should positively support those conclusions with no
other impact.

No complexity must be added to address that. The code approach is
actually shorter:

String(obj); // IE Errors.
("" + obj); // No errors.

The solution supports the conclusions that a consistent return type is
good and inconsistent errors are bad. The solution has a slight positive
impact on the code in that it is shorter.

It looks like a win-win situation. Is there a reason for favoring the
String constructor in this case? Given the evidence, it looks like a win
for string concatenation, starting with the empty string.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/
From: Thomas 'PointedEars' Lahn on
Garrett Smith wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Garrett Smith wrote:
>>> Thomas 'PointedEars' Lahn wrote:
>>>> Garrett Smith wrote:
>>>>> Thomas 'PointedEars' Lahn wrote:
>>>>>> return String(this).replace(/^\s+|\s+$/g, "");
> [...]
>>> The Error is unacceptable. That leaves two options. The more sensible
>>> output is the empty string in this case.
>>
>> The more sensible approach is not to do that in the first place.
>> Which part of
>
> Try to differentiate between what the caller should not be doing vs how
> the function handles the caller doing that.

Try to understand what you are proposing.

>>>> Host objects SHOULD NOT be augmented with properties, or be used as
>>>> first argument to Function.prototype.call() or
>>>> Function.prototype.apply(). Your argument is not logical.
>>
>> did you not get?
>
> I understand and agree. However, it is reasonable to allow for an object
> to be the first argument to `trim`.

What the heck are you talking about? String.prototype.trim() takes no
arguments per ES5!

> [non sequitur]


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>