has-own.js 1.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --harmony-object-has-own

let object = { foo: false };
assertTrue((Object.hasOwn(object, "foo")));

let object2 = Object.create({ foo: true });
assertFalse(Object.hasOwn(object2, "foo"));

let object3 = Object.create(null);
assertFalse(Object.hasOwn(object3, "foo"));

let O = function() {
  this.foo = 42;
};
let object8 = new O;
assertTrue((Object.hasOwn(object8, "foo")));

// Object is undefined or null
assertThrows(() => Object.hasOwn(undefined). TypeError);
assertThrows(() => Object.hasOwn(null). TypeError);

// Using defineProperty
let object4 = {};
Object.defineProperty(object4, "foo", {
  value: 42
});
assertTrue((Object.hasOwn(object4, "foo")));

// Object.defineProperty with base
let base = {};
Object.defineProperty(base, "foo", {
  value: 42,
  configurable: true
});
let object5 = Object.create(base);
assertTrue((Object.hasOwn(base, "foo")));
assertFalse((Object.hasOwn(object5, "foo")));

// Prototype with getter and/or setter
let object6 = {
  get foo() {}
};
let object7 = {
  set foo(x) {}
};
assertTrue((Object.hasOwn(object6, "foo")));
assertTrue((Object.hasOwn(object7, "foo")));