检测 _ 是 lodash 还是下划线
Detecting if _ is lodash or underscore
检测 _
变量是否加载了 lodash 或下划线的 "authoritative" 方法是什么?
我正在使用lodash for a project in an environment where underscore可能有时候也会加载
目前,我想出了这个:
/**
* lodash defines a variable PLACEHOLDER = '__lodash_placeholder__'
* so check if that is defined / contains the string "lodash"
*/
if ( typeof( _.PLACEHOLDER ) == 'undefined' || _.PLACEHOLDER.indexOf( 'lodash' ) < 0 ) {
// _ is underscore, do what I need to access lodash
}
重要更新:以上代码无效!
是否有 "authoritative" 方法来检测 _
是 lodash 还是下划线?
备注:
这是一个特定的请求,希望找到一种方法来确定 _
变量中是否加载了 lodash 或下划线:
1.下划线是否加载是我无法控制的。 (lodash 在我的控制范围内,并且会一直加载)。
2.lodash/underscore的加载顺序不能靠谱
3.加载的underscore版本可能会改变(它是可能更新的CMS框架的一部分)。
4. Lodash 4.17.x 有 300 多个函数。我的代码使用了 lodash 中的 许多 函数。
5. Lodash包含许多下划线没有提供的功能。
6. 两个库中确实存在的一些函数有不同的实现。
与@bhantol 已经指出的类似,有一个 Migrating doc 列出了 lodash 和下划线之间的差异,不 与之兼容。那些不能用吗?例如,
if ( typeof( _.invoke ) !== 'undefined' ){
// it's lodash
}
但是,是的,放大@felix-kling 和@tadman 以及其他人的评论,如果可能的话,将问题限制在功能(例如:特定方法)级别而不是整个库可能更可靠。
问题 中发布的代码不起作用 ,因为 PLACEHOLDER
是一个私有变量,在缩小过程中被重命名。
因此,我采用了评论中提到的 "feature detection" 的概念。请注意,如果下划线的未来版本在所有这些函数中滚动,或者如果 lodash 弃用这些函数中的任何一个,则此方法可能会崩溃:
var isLodash = false;
// If _ is defined and the function _.forEach exists then we know underscore OR lodash are in place
if ( 'undefined' != typeof(_) && 'function' == typeof(_.forEach) ) {
// A small sample of some of the functions that exist in lodash but not underscore
var funcs = [ 'get', 'set', 'at', 'cloneDeep' ];
// Simplest if assume exists to start
isLodash = true;
funcs.forEach( function ( func ) {
// If just one of the functions do not exist, then not lodash
isLodash = ('function' != typeof(_[ func ])) ? false : isLodash;
} );
}
if ( isLodash ) {
// We know that lodash is loaded in the _ variable
console.log( 'Is lodash: ' + isLodash );
} else {
// We know that lodash is NOT loaded
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.3/lodash.js"></script>
检测 _
变量是否加载了 lodash 或下划线的 "authoritative" 方法是什么?
我正在使用lodash for a project in an environment where underscore可能有时候也会加载
目前,我想出了这个:
/**
* lodash defines a variable PLACEHOLDER = '__lodash_placeholder__'
* so check if that is defined / contains the string "lodash"
*/
if ( typeof( _.PLACEHOLDER ) == 'undefined' || _.PLACEHOLDER.indexOf( 'lodash' ) < 0 ) {
// _ is underscore, do what I need to access lodash
}
重要更新:以上代码无效!
是否有 "authoritative" 方法来检测 _
是 lodash 还是下划线?
备注:
这是一个特定的请求,希望找到一种方法来确定 _
变量中是否加载了 lodash 或下划线:
1.下划线是否加载是我无法控制的。 (lodash 在我的控制范围内,并且会一直加载)。
2.lodash/underscore的加载顺序不能靠谱
3.加载的underscore版本可能会改变(它是可能更新的CMS框架的一部分)。
4. Lodash 4.17.x 有 300 多个函数。我的代码使用了 lodash 中的 许多 函数。
5. Lodash包含许多下划线没有提供的功能。
6. 两个库中确实存在的一些函数有不同的实现。
与@bhantol 已经指出的类似,有一个 Migrating doc 列出了 lodash 和下划线之间的差异,不 与之兼容。那些不能用吗?例如,
if ( typeof( _.invoke ) !== 'undefined' ){
// it's lodash
}
但是,是的,放大@felix-kling 和@tadman 以及其他人的评论,如果可能的话,将问题限制在功能(例如:特定方法)级别而不是整个库可能更可靠。
问题 中发布的代码不起作用 ,因为 PLACEHOLDER
是一个私有变量,在缩小过程中被重命名。
因此,我采用了评论中提到的 "feature detection" 的概念。请注意,如果下划线的未来版本在所有这些函数中滚动,或者如果 lodash 弃用这些函数中的任何一个,则此方法可能会崩溃:
var isLodash = false;
// If _ is defined and the function _.forEach exists then we know underscore OR lodash are in place
if ( 'undefined' != typeof(_) && 'function' == typeof(_.forEach) ) {
// A small sample of some of the functions that exist in lodash but not underscore
var funcs = [ 'get', 'set', 'at', 'cloneDeep' ];
// Simplest if assume exists to start
isLodash = true;
funcs.forEach( function ( func ) {
// If just one of the functions do not exist, then not lodash
isLodash = ('function' != typeof(_[ func ])) ? false : isLodash;
} );
}
if ( isLodash ) {
// We know that lodash is loaded in the _ variable
console.log( 'Is lodash: ' + isLodash );
} else {
// We know that lodash is NOT loaded
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.3/lodash.js"></script>