summaryrefslogtreecommitdiff
path: root/js/util.ts
diff options
context:
space:
mode:
author迷渡 <justjavac@gmail.com>2019-04-19 09:56:33 +0800
committerRyan Dahl <ry@tinyclouds.org>2019-04-18 21:56:33 -0400
commitd0cd7a39a2d05343c5501dc286bb59096659654f (patch)
treeadffb215b15e8ca3411db732f5604158e5ab9640 /js/util.ts
parent2be7e4440339f616a37b9535cc38936f80efc0e1 (diff)
avoid prototype builtin hasOwnProperty (#2144)
Diffstat (limited to 'js/util.ts')
-rw-r--r--js/util.ts30
1 files changed, 30 insertions, 0 deletions
diff --git a/js/util.ts b/js/util.ts
index e6f38c8e1..b81b96aca 100644
--- a/js/util.ts
+++ b/js/util.ts
@@ -142,3 +142,33 @@ export function getPrivateValue<
}
throw new TypeError("Illegal invocation");
}
+
+/**
+ * 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
+ * @internal
+ */
+export function hasOwnProperty<T>(obj: T, v: PropertyKey): boolean {
+ if (obj == null) {
+ return false;
+ }
+ return Object.prototype.hasOwnProperty.call(obj, v);
+}