summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
author迷渡 <justjavac@gmail.com>2019-09-03 15:10:05 +0800
committerRyan Dahl <ry@tinyclouds.org>2019-09-03 03:10:05 -0400
commit9533a030f34097d5ae91c0fd9009578b3932bbea (patch)
treedb4fa9f7474cecbc08048eca8e7c26a87b80d61e /util
parentdd6db011e3a93f508f49ce1de84962a4ca23c587 (diff)
Avoid prototype builtin `hasOwnProperty` (denoland/deno_std#577)
Original: https://github.com/denoland/deno_std/commit/d36bff3fbe22a3585d23213e3f4c9f58756c032b
Diffstat (limited to 'util')
-rw-r--r--util/has_own_property.ts30
1 files changed, 30 insertions, 0 deletions
diff --git a/util/has_own_property.ts b/util/has_own_property.ts
new file mode 100644
index 000000000..707d951d3
--- /dev/null
+++ b/util/has_own_property.ts
@@ -0,0 +1,30 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+
+/**
+ * Determines whether an object has a property with the specified name.
+ * Avoid calling prototype builtin `hasOwnProperty` for two reasons:
+ *
+ * 1. `hasOwnProperty` is defined on the object as something else:
+ *
+ * const options = {
+ * ending: 'utf8',
+ * hasOwnProperty: 'foo'
+ * };
+ * options.hasOwnProperty('ending') // throws a TypeError
+ *
+ * 2. The object doesn't inherit from `Object.prototype`:
+ *
+ * const options = Object.create(null);
+ * options.ending = 'utf8';
+ * options.hasOwnProperty('ending'); // throws a TypeError
+ *
+ * @param obj A Object.
+ * @param v A property name.
+ * @see https://eslint.org/docs/rules/no-prototype-builtins
+ */
+export function hasOwnProperty<T>(obj: T, v: PropertyKey): boolean {
+ if (obj == null) {
+ return false;
+ }
+ return Object.prototype.hasOwnProperty.call(obj, v);
+}