or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

has

has
provides a safe shortcut for
Object.prototype.hasOwnProperty.call()
, offering a reliable way to check if an object has its own property without the risks associated with objects that may have overridden the
hasOwnProperty
method or objects created with null prototype.

Package Information

  • Package Name: has
  • Package Type: npm
  • Language: JavaScript
  • Installation:
    npm install has

Core Imports

var has = require('has');

Note: This package only supports CommonJS (no ESM support).

Basic Usage

var has = require('has');

// Check if an object has its own property
has({}, 'hasOwnProperty');                    // false
has(Object.prototype, 'hasOwnProperty');      // true

// Safe property checking for objects that may lack hasOwnProperty
var obj = Object.create(null);
obj.foo = 'bar';
has(obj, 'foo');                              // true
has(obj, 'hasOwnProperty');                   // false

// Works safely with all object types
var arr = [1, 2, 3];
has(arr, 'length');                           // true  
has(arr, '0');                                // true
has(arr, 'push');                             // false (inherited method)

Capabilities

Property Existence Checking

Safe property ownership checking that works with all object types, including those with overridden

hasOwnProperty
or null prototypes.

/**
 * Safely checks if an object has its own property
 * @param {Object} O - The object to check for property ownership  
 * @param {string|symbol} P - The property name to check
 * @returns {boolean} true if object has own property, false otherwise
 * @throws {TypeError} when object is null or undefined
 */
function has(O, P);

Usage Examples:

var has = require('has');

// Standard objects
has({ name: 'John' }, 'name');               // true
has({ name: 'John' }, 'toString');           // false (inherited)

// Objects with overridden hasOwnProperty  
var obj = { hasOwnProperty: 'not a function' };
obj.name = 'test';
has(obj, 'name');                            // true (safe)

// Objects created with null prototype
var nullObj = Object.create(null);
nullObj.prop = 'value';
has(nullObj, 'prop');                        // true

// Arrays and array-like objects
has(['a', 'b'], '0');                        // true
has(['a', 'b'], 'length');                   // true
has(['a', 'b'], 'push');                     // false

// Error cases
has(null, 'prop');                           // throws TypeError
has(undefined, 'prop');                      // throws TypeError

Error Handling

The function throws a

TypeError
when the first argument is
null
or
undefined
, matching the behavior of the native
hasOwnProperty
method:

try {
  has(null, 'property');
} catch (error) {
  console.log(error instanceof TypeError); // true
}

try {
  has(undefined, 'property');  
} catch (error) {
  console.log(error instanceof TypeError); // true
}

Implementation Details

The function uses

Function.prototype.call.bind()
when available for optimal performance, falling back to a manual binding approach for older JavaScript environments. This ensures compatibility across different JavaScript engines while maintaining safety and performance.

Types

For TypeScript users, the function signature is effectively:

function has(O: any, P: string | symbol): boolean;

Note: This package does not include TypeScript definitions. Consider using

@types/has
if TypeScript support is needed.