Gets the native function at key of object, ensuring they are truly native implementations.
npx @tessl/cli install tessl/npm-lodash--getnative@3.9.0A 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.
npm install lodash._getnativevar getNative = require('lodash._getnative');From the lodash monorepo context:
var getNative = require('./internal/getNative');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');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:
isNative utility to verify the value is a genuine native functionundefined otherwiseUsage 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');This package relies on the following internal utilities:
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:
false for null/undefined valuesImplementation Notes:
The isNative function internally uses escapeRegExp for safe regex pattern creation and isObjectLike for object-type validation, ensuring robust cross-environment compatibility.
// Parameter types
type ObjectToQuery = Object | null | undefined;
type KeyToRetrieve = string;
// Return types
type NativeFunctionResult = Function | undefined;
type IsNativeResult = boolean;The getNative utility provides several security benefits:
The utility is designed to never throw errors:
undefinedundefined after failing the native verification