Should isHostMethod be added to the FAQ?

T

Thomas 'PointedEars' Lahn

kangax said:
Thomas said:
kangax said:
Thomas 'PointedEars' Lahn wrote:
for (var i = 1; i < len; i++)
{
var
p = arguments,
p_is_Array = (i === len - 1 && jsx.object.isArray(p)),
origP = p;
[...]
}

Did I miss something?
[...] avoid resolving `jsx.object.isArray` over and over,

Will do (as done elsewhere). However, it would have to be v.isArray(...)
in any case, or extensive rewrites might be needed if I change how
jsx.object.isArray() gets to the jsx.object.isInstanceOf() that it calls.


Hmm. I was thinking of something like `var isArray =
jsx.object.isArray`. So I take it that `isArray` is using `this` to
reference `jsx.object`?


It might, in the future, when the global `isArray' variable (not that here)
were no longer needed.
If so, then introducing extra variable to go
from double property access to single doesn't seem justified.

It is not just the property access but also the identifier resolution along
the scope chain. The additional steps, and unreliability avoided, by using

function ...(...)
{
var foo = bar.baz;

for (...)
{
foo.bla
}
}

instead of

function ...(...)
{
for (...)
{
bar.baz.bla
}
}

are worth closer inspection.
As far as type converting host objects (not methods!), didn't we just
have a conversation about it where you said that it should be safe to do so?

[...]
<http://groups.google.com/group/comp.lang.javascript/msg/4df3cf9b5a7bd60f>

Or am I missing something?
Yes, I was talking about return values of standardized methods there.
I'm pretty sure we were talking about `lastChild`/`firstChild` as an
operand in `if`/`while` expression (when "emptying" element contents).
You're right. However, I would submit that there is still a difference
between a property of a host object that must strictly follow specification,
and a host-defined property of the global object or a property of a host
object, that does not strictly follows implementation (for historical reasons).

So `Node::firstChild` is part of a DOM standard, but `Window::document`
is not (because global `window` is not part of a standard). Am I
understanding you correctly?
Partially.
It is not the value but the existence of the property that matters here.

Just to make things clear, I am interested in your opinion about
boolean- type conversion of host objects (not methods). You mentioned
that it is safe to type convert `<nodeRef>.firstChild/lastChild`.

No, I said that I could not see a good reason why it should be unsafe.
Now I'm trying to figure out whether it is safe to type-convert other host
objects, such as `document`;

e.g. - `if (<global object>.window.document) { ... }`.

The specified arbitrariness of host object's [[Get]] implementations
suggests that it is probably is not safe.
I usually use `null`.

I would rather throw an exception instead. `is*' methods are not supposed
to return non-booleans; and if you replied that types would not matter, my
reply would be why `false' would not suffice then.

Actually, in a conforming implementation of ECMAScript, Edition 3, it is not
that inefficient. While the initialization of the variable happens on each
call indeed, the creation of the RegExp object would not (section 7.8.5).

ECMAScript Edition 3.5 Final Draft, section 7.8.5, suggests that this
behavior might change. But as for now, only if the runtime of the
initialization turned out to be significant, then another closure might be
in order.
I would use a closure (into which things like `rxUnknown` and `rxMethod`
can go), and if closure is out of the question, use function object
itself, but access it via identifier, not `arguments.callee`.
^^^^^^^^^^^^^^^^^^^^^^^^
Read again.


PointedEars
 
T

Thomas 'PointedEars' Lahn

kangax said:
Thomas said:
kangax said:
Now I'm trying to figure out whether it is safe to type-convert other host
objects, such as `document`;

e.g. - `if (<global object>.window.document) { ... }`.
The specified arbitrariness of host object's [[Get]] implementations
suggests that it is probably is not safe.

That's exactly why I couldn't understand why `document` should be
guarded against [[Get]] but `firstChld`/`lastChild` not. Both elements
can implement it as implementation desires (including throw).

But isn't ToBoolean(...document) more likely to do that than
ToBoolean(...first/lastChild)?
Because `false` indicates something else - that certain object didn't
pass a test. The fact that something went wrong during test itself (e.g.
not enough arguments) seems like a good candidate for a different return
value or (as you mentioned) a thrown exception.

if (isMethod() == null)

and the like is a self-fulfilling prophecy, isn't it?
Yes, of course. What I meant was variable initialization, evaluation of
regex literal to get a reference to regex object and assignment of that
reference to a corresponding variable. And all of that twice, for each
variable :)

Not much work, but can be avoided (especially if it's used in a method
that will be frequently called)

As I indicated, tests pending.


PointedEars
 

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,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top