2

下劃線模板不調試器

// run with console open 
 
//and paste following when you hit the debugger: 
 

 
/* 
 
_.templateSettings = { 
 
    interpolate: /\{\{(.+?)\}\}/g 
 
}; 
 

 
var template = _.template("Hello {{ name }}!"); 
 

 
console.log(template({name: "Mustache"})) 
 
*/ 
 
debugger 
 
//should return: 
 
//underscore-min.js:5Uncaught TypeError: Cannot read property 'call' of undefined 
 

 
//out of debugger though, it works: 
 
_.templateSettings = { 
 
    interpolate: /\{\{(.+?)\}\}/g 
 
}; 
 
var template = _.template("Hello {{ name }}!"); 
 
console.log(template({name: "Mustache"}))
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

工作,我不能運行underscore's sample template代碼,同時在調試器(我想在控制檯實際數據播放)。在.js文件

  1. 代碼運行正常。 ✓在頁面加載運行正常後,將
  2. 粘貼到控制檯中。 ✓
  3. 在調試器斷點時粘貼 - 不工作。 ✘

    _.templateSettings = { 
        interpolate: /\{\{(.+?)\}\}/g 
    } 
    
    var template = _.template("Hello {{ name }}!"); 
    
    template({name: "Mustache"}); 
    

    錯誤:

    underscore.js:1461 Uncaught TypeError: Cannot read property 'call' of undefined 
    

編輯:於template({name: "Mustache"});

的下劃線版本1.8.3 Line 1461


錯誤:

var template = function(data) { 
    return render.call(this, data, _); 
}; 
+1

下劃線的哪個版本?粘貼代碼中的哪一行觸發錯誤?您正在使用的underscore.js版本的第1461行是什麼? –

+0

已回答(請參閱編輯) – Ashbury

+0

斷點在哪裏?它是否在下劃線? –

回答

2

這是Function實例(下劃線線1454)的症狀,它可以使用下面的語句來重新創建:

new Function('return true;') 

在瀏覽器中打開的任何網頁(甚至這一個),複製此到控制檯並執行,將打印如下:

function anonymous() { 
    return true; 
} 

但是,如果該頁面目前調試器在S已暫停陳述返回undefined。我試着在Firefox中運行相同的程序,它也不起作用,甚至不會返回。

當他們暫停在調試器,我無法解釋在V8(鉻)或SpiderMonkey的(Firefox)的JavaScript引擎的作用。

如果你真的需要它,這裏有一個解決方法:https://jsfiddle.net/na4Lpt7L/

var source = "Hello {{ name }}!"; 
debugger; 
var template = _.template(source); 
debugger; 

當第一個斷點命中:

> source = "Goodbye {{ name }}!"; 
< "Goodbye {{ name }}!" 

現在繼續執行,並在第二個斷點:

> template({name: "Mustache"}); 
< "Goodbye Mustache!" 

如果你想嘗試幾個選項,我會說堅持它在一個循環(while (source.length) { ... }

+0

你見過[視頻](https://vimeo.com/248018117)?密碼是Passsword [文件](https://ufile.io/okxmk)文件在這裏沒有調試器;斷點或甚至console.log裏面_.template你知道爲什麼 –