以下是我尝试使用下划线运行的小代码:
var data = [
{"name": "Romania"},
{"name": "Virgin Islands, British"},
{"name": "Gibraltar"},
{"name": "Bangladesh"},
{"name": "Taiwan"},
{"name": "Jamaica"},
{"name": "Paraguay"},
{"name": "Reunion"},
{"name": "Marshall Islands"},
{"name": "Nepal" },
{ "name": "Ireland" }
];
var namelist = _.pluck(data, "name");
console.log("list 1 => " + namelist);
// outputs => list 1 => Romania,Virgin Islands, British...
(function(underscore){
var u = underscore.noConflict();
var namelist = u.pluck(data, "name");
console.log("list 2 => " + namelist);
// outputs => list 2 => Romania,Virgin Islands, British...
})(_);
var namelist = _.pluck(data, "name");
console.log("list 3 => " + namelist);
// throw an error: Uncaught TypeError: Cannot read property 'pluck' of undefined
与jquery不同,我们通常使用noConflict方法避免两个jquery版本或其他库的$之间的冲突,不知何故我不明白使用 noConflict
的明确含义这就是我的页面中的情况。不同的模板和不同的语法,因此我尝试使用不同模板设置的template
underscore
方法。
我做错了吗? 如何在不影响全局变量的情况下获得下划线的新实例?
谢谢!
答案 0 :(得分:1)