or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--getnative

Gets the native function at key of object, ensuring they are truly native implementations.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash._getnative@3.9.x

To install, run

npx @tessl/cli install tessl/npm-lodash--getnative@3.9.0

index.mddocs/

lodash._getnative

A lodash utility function that safely retrieves native JavaScript functions from objects, ensuring they are truly native implementations rather than polyfilled or overridden versions. This utility is particularly useful for safely accessing native browser APIs and built-in JavaScript functions while avoiding potential security issues.

Package Information

  • Package Name: lodash._getnative
  • Package Type: npm
  • Language: JavaScript (CommonJS)
  • Installation: npm install lodash._getnative

Core Imports

var getNative = require('lodash._getnative');

From the lodash monorepo context:

var getNative = require('./internal/getNative');

Basic Usage

var getNative = require('lodash._getnative');

// Get native Array.prototype.push if available and native
var nativePush = getNative(Array.prototype, 'push');
if (nativePush) {
    // Use the verified native function
    nativePush.call(myArray, item);
} else {
    // Fallback behavior when native function not available
    myArray.push(item);
}

// Check for native Object.hasOwnProperty
var hasOwnProperty = getNative(Object.prototype, 'hasOwnProperty');

// Check for native Map constructor
var NativeMap = getNative(window, 'Map');

Capabilities

getNative Function

Gets the native function at the specified key of an object, returning the function only if it's verified as a genuine native implementation.

/**
 * Gets the native function at `key` of `object`.
 *
 * @param {Object} object The object to query.
 * @param {string} key The key of the method to get.
 * @returns {*} Returns the function if it's native, else `undefined`.
 */
function getNative(object, key);

The function performs the following operations:

  1. Safely retrieves the value at the specified key from the object
  2. Uses the internal isNative utility to verify the value is a genuine native function
  3. Returns the function if native, or undefined otherwise

Usage Examples:

var getNative = require('lodash._getnative');

// Safe access to native functions
var nativeSlice = getNative(Array.prototype, 'slice');
var nativeHasOwnProperty = getNative(Object.prototype, 'hasOwnProperty');
var nativeSetTimeout = getNative(window, 'setTimeout');

// Handle cases where function doesn't exist or isn't native
var someFunction = getNative(customObject, 'customMethod');
if (someFunction) {
    // Use the verified native function
    someFunction.call(context, args);
} else {
    // Provide fallback implementation
    // ...
}

// Checking for browser APIs
var nativeFetch = getNative(window, 'fetch');
var nativePromise = getNative(window, 'Promise');

Internal Dependencies

This package relies on the following internal utilities:

isNative Function

Used internally by getNative to verify if a value is a native function.

/**
 * Checks if `value` is a native function.
 *
 * @param {*} value The value to check.
 * @returns {boolean} Returns `true` if `value` is a native function, else `false`.
 */
function isNative(value);

The verification process:

  1. Returns false for null/undefined values
  2. For function-type values, tests the function's string representation against a native function pattern
  3. For object-like values, tests against host constructor patterns (for browser environments)

Implementation Notes: The isNative function internally uses escapeRegExp for safe regex pattern creation and isObjectLike for object-type validation, ensuring robust cross-environment compatibility.

Type Definitions

// Parameter types
type ObjectToQuery = Object | null | undefined;
type KeyToRetrieve = string;

// Return types  
type NativeFunctionResult = Function | undefined;
type IsNativeResult = boolean;

Security and Safety

The getNative utility provides several security benefits:

  • Polyfill Protection: Ensures you're working with genuine native implementations, not polyfills that might have different behavior
  • Override Protection: Protects against malicious or accidental function overrides
  • Host Environment Safety: Safely handles browser-specific constructor patterns
  • Null Safety: Handles null/undefined objects gracefully without throwing errors

Error Handling

The utility is designed to never throw errors:

  • Null or undefined objects are handled safely, returning undefined
  • Invalid keys are handled gracefully
  • Non-function values at the specified key will return undefined after failing the native verification

Common Use Cases

  1. Framework Development: Lodash and other libraries use this to ensure they work with genuine native functions
  2. Polyfill Detection: Determine if a native implementation exists before loading polyfills
  3. Security-Conscious Code: Verify that critical functions haven't been tampered with
  4. Cross-Environment Compatibility: Safely detect native features across different JavaScript environments