Skip to content

Instantly share code, notes, and snippets.

@burdiuz
Last active October 28, 2021 20:15
Show Gist options
  • Save burdiuz/d81cd59d2d619d921531165fac0e8c77 to your computer and use it in GitHub Desktop.
Save burdiuz/d81cd59d2d619d921531165fac0e8c77 to your computer and use it in GitHub Desktop.
Functional replacement for Object.prototype.hasOwnProperty()

hasOwn

Failsafe function that uses Object.prototype.hasOwnProperty() and return false in case if target is not a valid object.

Note: It uses arrow functions that may not be supported by some old environments.

You better use lodash/has.

export function hasOwn(target: any, name: string | symbol): boolean;
export const hasOwn = (
(has) =>
(target, property) =>
Boolean(target && has.call(target, property))
)(Object.prototype.hasOwnProperty);
export default hasOwn;
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const hasOwn = (
(has) =>
(target, property) =>
Boolean(target && has.call(target, property))
)(Object.prototype.hasOwnProperty);
exports.hasOwn = hasOwn;
exports.default = hasOwn;
{
"name": "@actualwave/has-own",
"description": "Functional replacement for Object.prototype.hasOwnProperty()",
"version": "0.0.3",
"main": "index.js",
"module": "index.es.js",
"types": "index.d.ts",
"keywords": [
"js",
"javascript",
"Object",
"hasOwnProperty",
"has",
"hasOwn"
],
"homepage": "https://gist.github.com/burdiuz/d81cd59d2d619d921531165fac0e8c77",
"bugs": {
"url": "https://gist.github.com/burdiuz/d81cd59d2d619d921531165fac0e8c77",
"email": "burdiuz@gmail.com"
},
"license": "MIT",
"author": "Oleg Galaburda <burdiuz@gmail.com> (http://actualwave.com/)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment