1. 程式人生 > >javascript create alias for document.getElmentById, 通過apply修改this指向

javascript create alias for document.getElmentById, 通過apply修改this指向

* 通過apply修改this指向 

  window, 改為document

document.getElementById = function(fun) {
	return function() {
		return fun.apply(document, arguments);
	}
}(document.getElementById);
var getId = document.getElementById;

// usage:

getId("iusername");

但是這麼寫 是undefined

var byId = function(fun) {
    return function(id) {
        fun.call(document, id);
    };
}(document.getElementById);

var u = byId("iusername");
console.log(u);    // undefined