or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md
tile.json

index.mddocs/

0

# lodash._getnative

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: lodash._getnative

7

- **Package Type**: npm

8

- **Language**: JavaScript (CommonJS)

9

- **Installation**: `npm install lodash._getnative`

10

11

## Core Imports

12

13

```javascript

14

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

15

```

16

17

From the lodash monorepo context:

18

19

```javascript

20

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

21

```

22

23

## Basic Usage

24

25

```javascript

26

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

27

28

// Get native Array.prototype.push if available and native

29

var nativePush = getNative(Array.prototype, 'push');

30

if (nativePush) {

31

// Use the verified native function

32

nativePush.call(myArray, item);

33

} else {

34

// Fallback behavior when native function not available

35

myArray.push(item);

36

}

37

38

// Check for native Object.hasOwnProperty

39

var hasOwnProperty = getNative(Object.prototype, 'hasOwnProperty');

40

41

// Check for native Map constructor

42

var NativeMap = getNative(window, 'Map');

43

```

44

45

## Capabilities

46

47

### getNative Function

48

49

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

50

51

```javascript { .api }

52

/**

53

* Gets the native function at `key` of `object`.

54

*

55

* @param {Object} object The object to query.

56

* @param {string} key The key of the method to get.

57

* @returns {*} Returns the function if it's native, else `undefined`.

58

*/

59

function getNative(object, key);

60

```

61

62

The function performs the following operations:

63

1. Safely retrieves the value at the specified key from the object

64

2. Uses the internal `isNative` utility to verify the value is a genuine native function

65

3. Returns the function if native, or `undefined` otherwise

66

67

**Usage Examples:**

68

69

```javascript

70

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

71

72

// Safe access to native functions

73

var nativeSlice = getNative(Array.prototype, 'slice');

74

var nativeHasOwnProperty = getNative(Object.prototype, 'hasOwnProperty');

75

var nativeSetTimeout = getNative(window, 'setTimeout');

76

77

// Handle cases where function doesn't exist or isn't native

78

var someFunction = getNative(customObject, 'customMethod');

79

if (someFunction) {

80

// Use the verified native function

81

someFunction.call(context, args);

82

} else {

83

// Provide fallback implementation

84

// ...

85

}

86

87

// Checking for browser APIs

88

var nativeFetch = getNative(window, 'fetch');

89

var nativePromise = getNative(window, 'Promise');

90

```

91

92

## Internal Dependencies

93

94

This package relies on the following internal utilities:

95

96

### isNative Function

97

98

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

99

100

```javascript { .api }

101

/**

102

* Checks if `value` is a native function.

103

*

104

* @param {*} value The value to check.

105

* @returns {boolean} Returns `true` if `value` is a native function, else `false`.

106

*/

107

function isNative(value);

108

```

109

110

The verification process:

111

1. Returns `false` for null/undefined values

112

2. For function-type values, tests the function's string representation against a native function pattern

113

3. For object-like values, tests against host constructor patterns (for browser environments)

114

115

**Implementation Notes:**

116

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

117

118

### Type Definitions

119

120

```javascript { .api }

121

// Parameter types

122

type ObjectToQuery = Object | null | undefined;

123

type KeyToRetrieve = string;

124

125

// Return types

126

type NativeFunctionResult = Function | undefined;

127

type IsNativeResult = boolean;

128

```

129

130

## Security and Safety

131

132

The `getNative` utility provides several security benefits:

133

134

- **Polyfill Protection**: Ensures you're working with genuine native implementations, not polyfills that might have different behavior

135

- **Override Protection**: Protects against malicious or accidental function overrides

136

- **Host Environment Safety**: Safely handles browser-specific constructor patterns

137

- **Null Safety**: Handles null/undefined objects gracefully without throwing errors

138

139

## Error Handling

140

141

The utility is designed to never throw errors:

142

- Null or undefined objects are handled safely, returning `undefined`

143

- Invalid keys are handled gracefully

144

- Non-function values at the specified key will return `undefined` after failing the native verification

145

146

## Common Use Cases

147

148

1. **Framework Development**: Lodash and other libraries use this to ensure they work with genuine native functions

149

2. **Polyfill Detection**: Determine if a native implementation exists before loading polyfills

150

3. **Security-Conscious Code**: Verify that critical functions haven't been tampered with

151

4. **Cross-Environment Compatibility**: Safely detect native features across different JavaScript environments