is lodash or underscore

What is an "authoritative" way to detect if the _ variable is loaded with lodash or underscore?

I am using lodash for a project in an environment where underscore may sometimes also be loaded.

Currently, I've come up with this:

/** 
 * 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
}

Important update: The above code does NOT work!

Is there an "authoritative" way to detect if _ is lodash or underscore?

Notes:
This is a specific request to find a way to determine if lodash or underscore is loaded in the _ variable:
1. It is outside of my control whether underscore is loaded or not. (lodash is within my control, and will always be loaded).
2. The load order of lodash / underscore can not be relied upon.
3. The version of underscore that is loaded is likely to change (it's part of a CMS framework that may be updated).
4. Lodash 4.17.x has 300+ functions. My code utilizes many of the functions in lodash.
5. Lodash contains many functions that underscore does not provide.
6. Some of the functions that do exist in both libraries have different implementations.


Going along similar lines as @bhantol already noted, there is a Migrating doc with a list of differences between lodash and underscore that were not compatibilized with. Can't those be used? For example,

if ( typeof( _.invoke ) !== 'undefined' ){
    // it's lodash
}

But yeah, amplifying comments by @felix-kling and @tadman and others, if possible, it might be more reliable to restrict the question to the feature (eg: specific method) level rather than the whole library.


The code posted in the question does not work, as PLACEHOLDER is a private variable that gets renamed during minification.

Therefore, I've adapted the notion of "feature detection" as mentioned in the comments. Note that this method might break down if future versions of underscore roll in all of these functions, or if lodash deprecates any of these functions:

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>
链接地址: http://www.djcxy.com/p/94466.html

上一篇: 使用Identity Server 4保护.NET框架4.x web API

下一篇: 是lodash或下划线