Javascript 深入瞭解Javascript 基礎知識

來源:互聯網
上載者:User

標籤:javascript基礎   前端開發   

eval全域函數dojo 的載入器中會看到以下一個函數
   var eval_ = new Function('return eval(arguments[0]);'); //Function 函數是在頂級範圍下運行, 但運行效率更慢, 但使用這種方法不會汙染全域變數,並且調用的是頂級範圍    eval('var i =100;');    alert(i); // 100    eval_('var b = 100');    //alert(b); //錯誤, b 沒有定義    var where = '我在國外'; //全域範圍的where    function test() {        var where = '我在國內'; //閉包的where        eval_('alert(where)'); //我在國外, 全域範圍        eval('alert(where)');//我在國內, 局部全用域        window.eval('alert(where)'); //我在國外 IE6/7/8 我在國內,不會採用全域範圍, 可以使用execScript    }    test()

dojo的kernal模組中也會有一個eval函數, 代碼和解釋如下 
(Function("d", "d.eval = function(){return d.global.eval ? d.global.eval(arguments[0]) : eval(arguments[0]);}"))(dojo);/*=====dojo.eval = function(scriptText){//  嘗試在全域範圍下執行角本字串(scriptText), 除了IE不能支援,其它瀏覽器都可以正確的在全域範圍下執行, IE下執行    dojo.eval('var pi = 3.14');    alert(pi)  //pi undefined;            要在IE中執行全域變數            dojo.eval("window.pi = 3.14");                IE 下要執行全域變數, 只能使用execScript, 但是它不會傳回值, 並且不能終止執行*/


Javascript 深入瞭解Javascript 基礎知識

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.