引用getElementById方法报错NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript ar

想仿照jquery的方法弄个$出来,写了以下js
<script type="text/javascript">
	window.onload = function() {
		g = document.getElementById;
		
		alert(document.getElementById('div1'));
		alert(g('div1'));
	}
</script>

结果g('div1')这个在火狐下报错
NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument
在stackoverflow上查到了答案
http://stackoverflow.com/questions/8773579/if-javascript-has-first-class-functions-why-doesnt-this-work

原来getElementById方法中可能使用了this,这个this指的是document,
上面那样直接g = document.getElementById;this就会是global了

你可能感兴趣的:(JavaScript)