<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <meta charset="utf-8" /> <script src="../jquery.js"></script> <script> $(function () { // 測試 null的指向 var func = function () { // 說明當call 的第一個參數爲null是, 指向是window , //console.log(arguments); //console.log(this); //console.log(this == window); //Array.prototype.join.apply(null, arguments); //console.log(arguments); } //func.call(null, 1, 2, 3); var funs = function (args) { //console.log(args); }; func.apply(funs, [1, 2, 3]); // 解決方案 document.getElementById = (function(func) { return function () { return func.apply(document, arguments); } })(document.getElementById); var getId = document.getElementById; console.log(getId('demo')); //document.getElementById.apply(document, 'demo'); //會棧溢出, /* 該實例輸出的結果是 Maximum call stack size exceeded document.getElementById = (function () { return function(){ return document.getElementById.apply(document, arguments); } })(); //var getIdss = document.getElementById; //console.log(getIdss('demo')); */ /* 簡化模型 輸出結果爲 Maximum call stack size exceeded */ /* (function a() { a(); })(); */ /* 問題說明: 1 棧溢出的錯誤緣由是 : 重複的調用了匿名函數, 79L : 函數在調用後, document.getElementById 函數被改寫, 76L : 函數調用的再也不是 原來的document.getElementById, 簡化後的函數體 document.getElementById= function(){ return document.getElementById.apply(document,argument); } 在執行 document.getElementById() 後, 函數開始本身調用本身了, // 簡化模式, 若是這麼調用也會出現 棧溢出的問題, var a = function () { return a.apply(window, arguments); } a(); 解決問題的思路 : 參考結局方案. 在運行時 func 爲一個函數 getElementById(){}; */ /* document.getElementById = (function () { return function() { return document.getElementById.apply(window, arguments); } })(); var getId = document.getElementById; console.log(getId('demo')); */ }); </script> </head> <body> <div id="demo">demo</div> </body> </html>