document.getElementById介紹
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);
//執(zhí)行
alert(g('t'));
w();
});
在標(biāo)準(zhǔn)瀏覽器中(IE9、FF、chrome等)上述代碼執(zhí)行得非常一致,返回結(jié)果如下:
typeof => "function"
String => "function #funcName#{[native code]}"
instanceof Object => true
instanceof Function => true
很奇怪,雖然類型是函數(shù),但是我們卻不能直接使用括號來執(zhí)行函數(shù)g,而需要使用call
g.call(document,elementId);
但是如果運(yùn)行環(huán)境是IE6,一切看起來非常詭異,下面是運(yùn)行結(jié)果(注意粗體部分):
//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
在IE 6下,對于g和w都只能使用括號直接執(zhí)行函數(shù),而不需要使用call。對于函數(shù)g使用下面的方式調(diào)用會導(dǎo)致一個(gè)“對象沒有該屬性”的錯(cuò)誤:
g.call(document,eleId)
在IE6下,對于自定義的函數(shù)testFunc測試結(jié)果沒有任何問題,但是對于g卻十分地詭異!
既然g是object那么為何可以像函數(shù)一樣用()直接調(diào)用執(zhí)行?
而在標(biāo)準(zhǔn)瀏覽器中,g既然是函數(shù)為什么卻不能直接使用()來執(zhí)行呢?
事實(shí)上對于document.getElementById,它到底是function還是object就連jQuery 1.6.2也沒有解決這個(gè)問題。
在IE6中$.isFunction(g)仍然返回的是false!下面是jQuery 1.6.2的jQuery.isFunction的相關(guān)源代碼:
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";
}
于是在StackOverflow上提了這個(gè)問題,好在牛人確實(shí)多,很快就有了回復(fù)。最后我簡單的總結(jié)一下給大家參考:
document.getElementById 最初被定義為 HTMLDocument (HTML DOM)接口的一個(gè)成員,但是在后來的 2 級 DOM 中移入到 Document (XML DOM)接口中。
document.getElementById屬于host object,它是一個(gè)function,但是它并沒有被定義在ECMAScript中而是DOM接口的一部分。
支持[[Call]](內(nèi)部屬性?)host object的typeof返回值就是function。請記住Host Objects并不總是遵循Native Objects的相關(guān)規(guī)則,比如typeof。
而對于testFunc它是native object, 更具體地說是native function。
下面是EcmaScript 5對于typeof操作符的返回結(jié)果的歸類:
|
Type of val |
Result |
|
Undefined |
|
|
Null |
|
|
Boolean |
|
|
Number |
|
|
String |
|
|
Object (native and does not implement [[Call]]) |
|
|
Object (native or host and does implement [[Call]]) |
|
|
Object (host and does not implement [[Call]]) |
Implementation-defined except may not be |
var $ = function(id) { return document.getElementById(g) };
但是即使有了上面的解釋之后,我對Host Object和Native Object又有了新的疑惑。
- document.getElementById獲取控件對象為空的解決方法
- js中document.getElementByid、document.all和document.layers區(qū)分介紹
- jquery中的 $("#jb51")與document.getElementById("jb51") 的區(qū)別
- document.getElementById的簡寫方式(獲取id對象的簡略寫法)
- document.getElementById方法在Firefox與IE中的區(qū)別
- document.getElementById為空或不是對象的解決方法
- 各瀏覽器對document.getElementById等方法的實(shí)現(xiàn)差異解析
相關(guān)文章
重寫document.write實(shí)現(xiàn)無阻塞加載js廣告(補(bǔ)充)
這篇文章主要介紹了重寫document.write實(shí)現(xiàn)無阻塞加載js廣告,需要的朋友可以參考下2014-12-12
JavaScript實(shí)現(xiàn)常用二級省市級聯(lián)下拉列表的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)常用二級省市級聯(lián)下拉列表的方法,實(shí)例分析了javascript實(shí)現(xiàn)級聯(lián)下拉列表的技巧與相關(guān)的元素操作方法,需要的朋友可以參考下2015-03-03
深入理解javascript學(xué)習(xí)筆記(一) 編寫高質(zhì)量代碼
編寫高質(zhì)量JavaScript的一些要素,例如避免全局變量,使用單變量聲明,在循環(huán)中預(yù)緩存length(長度),遵循代碼閱讀,以及更多2012-08-08
Javascript中Cookie的獲取和保存應(yīng)用案例
這篇文章主要給大家介紹了關(guān)于Javascript中Cookie的獲取和保存應(yīng)用的相關(guān)資料,Cookie是直接存儲在瀏覽器中的一小串?dāng)?shù)據(jù),文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
JavaScript獲取系統(tǒng)自帶的顏色選擇器功能(圖)
JavaScript獲取系統(tǒng)自帶的顏色選擇器功能,這個(gè)是針對IE瀏覽器,所以大家如果想用兼容性更好的代碼,可以查看腳本之家的相關(guān)文章。2010-08-08
JavaScript ES6中export、import與export default的用法和區(qū)別
這篇文章主要給大家介紹了JavaScript ES6中export、import與export default的用法和區(qū)別,文中介紹的非常詳細(xì),相信對大家學(xué)習(xí)ES6會有一定的幫助,需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03
js構(gòu)造函數(shù)constructor和原型prototype原理與用法實(shí)例分析
這篇文章主要介紹了js構(gòu)造函數(shù)constructor和原型prototype原理與用法,結(jié)合實(shí)例形式分析js構(gòu)造函數(shù)constructor和原型prototype基本原理、功能、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03

