开发者

what is document.getElementById?

consider th开发者_JAVA技巧e following code:

<script>
function testFunc(){
    alert('test')
}
$(function(){
    var g = document.getElementById , w = window.testFunc ;
    //g
    alert(typeof(g));
    alert(String(g));
    alert(g instanceof Object);
    alert(g instanceof Function);
    //w
    alert(typeof(w));
    alert(String(w));
    alert(w instanceof Object);
    alert(w instanceof Function);
    //run it
    alert(g('t'));
            w();

});
</script>

the code behaves the same in modern browser(chrome,IE 9,Firefox).And the result is:

typeof => "function"

String => "function #funcName#{[native code]}"

instanceof Object => true

instanceof Function => true

it is a little weird, we can easily invoke w by using (), but for g, we must invoke it like this:

g.call(document,elementId);

When it comes to IE 6, the result is totally diffrent:


//g

typeof => "object"

String => "function getElementById{[native code]}"

instanceof Object => false

instanceof Function => false

//w

typeof => "function"

String => "function testFunc{alert('test')}"

instanceof Object => true

instanceof Function => true

what is more,we must run g and w directly by using '()', and we can not invoke g like this:

g.call(document,'t')

this will cause an error. So here is my question: what is document.getElementById, function or object, and what is the diffrence between g and w?


document.getElementById is a host object and it is a function. It is not defined in EcmaScript but is part of the DOM interface.

4.3.8 host object

object supplied by the host environment to complete the execution environment of ECMAScript

Since it supports the [[Call]] operator it is also a function.

Host objects do not always obey the same rules as native objects w.r.t. typeof though section 11.4.3 of EcmaScript 5 has tightened the rules somewhat.

testFunc is a native object, specifically a native function.

4.3.6 native object

object in an ECMAScript implementation whose semantics are fully defined by this specification rather than by the host environment.

NOTE Standard native objects are defined in this specification. Some native objects are built-in; others may be constructed during the course of execution of an ECMAScript program.


This is a difference in user-agent implementation, one of the several hundreds one can find between IE6 and any other browser made after 1992. As long as the end functionality resembles the specifications, it makes very little difference how a user-agent accomplishes the functionality (except, of course, in performance)

Check out this chart of differences in implementation: http://webcoder.info/reference/BrowserFiltering.script.html

That said: don't support IE6. There's really no (and I mean NO) reason to worry about it any more than you'd worry about the AOL5.0 native browser.


If your underlying problem is cross-browser compatibility, why don't you just do

 var g = function(id) { return document.getElementById(g) };

to get a guranteed ordinary first-class function that works like getElementById?


document.getElementById is a function. IE6 is just out to lunch. Perhaps if you describe what problem you're really trying to solve, folks could help with that.


g is a native function in all browsers and w is not. IE shows a different string result because browsers will format functions as strings differently, and that's why you should never convert a function to a string in the first place.

I suppose IE6 shows the typeof a native function as object but it's not an instance of anything, since it is recognized and executed by the browser as a native function.

Doesn't really matter in the end though - you never need to know the type of document.getElementById, and especially not in such an old browser...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜