6.1 noConflict
6.1.1 语法
_.noConflict()
6.1.2 说明
放弃Underscore 的控制变量"_"。返回Underscore 对象的引用。
6.1.3 代码示例
var underscore = _.noConflict();
underscore.each('1', function(v) {
console.log(v); // String => 1
});
6.1.4 释放变量之后就不存在了
_.noConflict();
//释放变量之后变undefined
console.log(_); //=>undefined
//不可再次释放
_.noConflict(); // Cannot read property 'noConflict' of undefined
6.1.5 源码展示
看看是如何实现放弃变量控制的。
var root = this; //web里就是window
var previousUnderscore = root._; //先缓存起来
_.noConflict = function() {
root._ = previousUnderscore; //放弃变量控制的时候还原就好
return this; //返回_对象
};