Document. getElementById

Source: Internet
Author: User

Use your brain as a browser to execute the following code twice: IE6 and IE9: Copy codeThe Code is as follows: 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 );
// Execute
Alert (g ('T '));
W ();
});

In standard browsers (IE9, FF, chrome, etc.), the above code is executed very consistently and the returned results are as follows:
Typeof => "function"Copy codeThe code is as follows: String => "function # funcName # {[native code]}"
Instanceof Object => true
Instanceof Function => true

It is strange that although the type is a function, we cannot directly use brackets to execute the function g, instead of using call

G. call (document, elementId );
However, if the running environment is IE6, everything looks very strange. The following is the running result (note the bold part ):Copy codeThe Code is as follows: // 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

In IE 6, both g and w can only use parentheses to directly execute functions, rather than call. If function g is called in the following way, an error "the object does not have this attribute" is returned:
G. call (document, eleId)
In IE6, there is no problem with the test result of the custom function testFunc, but g is very strange!

Since g is an object, why can I directly call and execute it like a function?
In a standard browser, since g is a function, why cannot I directly use () for execution?
In fact, for document. getElementById, whether it is a function or object does not solve this problem even jQuery 1.6.2.
In IE6, $. isFunction (g) still returns false! The following is the source code of jQuery. isFunction of jQuery 1.6.2:

Copy codeThe Code is as follows: class2type = {};
...
// Populate the class2type map
JQuery. each ("Boolean Number String Function Array Date RegExp Object". split (""), function (I, name ){
Class2type ["[object" + name + "]"] = name. toLowerCase ();
});
...
Type: function (obj ){
Return obj = null?
String (obj ):
Class2type [Object. prototype. toString. call (obj)] | "object ";
},
...
IsFunction: function (obj ){
Return jQuery. type (obj) = "function ";
}

So I raised this question on StackOverflow. Fortunately, there were indeed many cool people and soon I got a reply. Finally, I will give a brief summary for your reference:
Document. getElementById was initially defined as a member of the HTMLDocument (html dom) interface, but was moved to the Document (xml dom) interface in Level 2 DOM later.
Document. getElementById belongs to the host object. It is a function, but it is not defined in ECMAScript but is part of the DOM interface.
[[Call] (internal attribute?) is supported ?) The typeof returned value of the host object is function. Remember that Host Objects does not always follow Native Objects rules, such as typeof.
For testFunc, It is a native object, more specifically native function.
The following is the classification of the returned results of the typeof operator EcmaScript 5:

TypeVal

Result

Undefined

"undefined"

Null

"object"

Boolean

"boolean"

Number

"number"

String

"string"

Object (native and does not implement [[Call])

"object"

Object (native or host and does implement [[Call])

"function"

Object (host and does not implement [[Call])

Implementation-defined partition T may not be"undefined","boolean","number", Or "string".

Therefore, to replace document. getElementById with $, you need to do this:Copy codeThe Code is as follows: var $ = function (id) {return document. getElementById (g )};

However, even with the above explanation, I have new doubts about Host Object and Native Object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.