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你知道为什么 –