[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty [Back]  [Original]

Object.prototype.hasOwnProperty() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Object.prototype.hasOwnProperty()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

Object hasOwnProperty() ( ) .

Note: Object.hasOwn() . hasOwnProperty() .

In this article

const object1 = {};
object1.property1 = 42;

console.log(object1.hasOwnProperty("property1"));
// Expected output: true

console.log(object1.hasOwnProperty("toString"));
// Expected output: false

console.log(object1.hasOwnProperty("hasOwnProperty"));
// Expected output: false

js
hasOwnProperty(prop)

prop

String .

true false .

hasOwnProperty() null undefined true . false . in , .

JavaScript . Object , . Array Object hasOwnProperty() .

js
const fruits = ["Apple", "Banana", "Watermelon", "Orange"];
fruits.hasOwnProperty(3); // true ('Orange')
fruits.hasOwnProperty(4); // false - not defined

Object.prototype null .

hasOwnProperty

example prop .

js
const example = {};
example.hasOwnProperty("prop"); // false

example.prop = "exists";
example.hasOwnProperty("prop"); // true - 'prop'   .

example.prop = null;
example.hasOwnProperty("prop"); // true -   null  .

example.prop = undefined;
example.hasOwnProperty("prop"); // true -   undefined  .

vs.

.

js
const example = {};
example.prop = "exists";

// `hasOwnProperty`   true .
example.hasOwnProperty("prop"); // true
example.hasOwnProperty("toString"); // false
example.hasOwnProperty("hasOwnProperty"); // false

// `in`       true .
"prop" in example; // true
"toString" in example; // true
"hasOwnProperty" in example; // true

.

js
const buz = {
  fog: "stack",
};

for (const name in buz) {
  if (buz.hasOwnProperty(name)) {
    console.log(`this is fog (${name}) for sure. Value: ${buz[name]}`);
  } else {
    console.log(name); // toString   
  }
}

for...in . hasOwnProperty . Object.getOwnPropertyNames() .

hasOwnProperty

JavaScript hasOwnProperty . .

js
const foo = {
  hasOwnProperty() {
    return false;
  },
  bar: "Here be dragons",
};

foo.hasOwnProperty("bar"); //    false .

Object.hasOwn() . hasOwnProperty .

js
const foo = { bar: "Here be dragons" };

// Object.hasOwn()  . - 
Object.hasOwn(foo, "bar"); // true

// Object  hasOwnProperty  
Object.prototype.hasOwnProperty.call(foo, "bar"); // true

//  Object hasOwnProperty 
// 'this' call  foo .
({}).hasOwnProperty.call(foo, "bar"); // true

.

Object.create(null)

null Object.prototype hasOwnProperty() .

js
const foo = Object.create(null);
foo.prop = "exists";
foo.hasOwnProperty("prop"); // Uncaught TypeError: foo.hasOwnProperty is not a function

. Object.hasOwn() hasOwnProperty() .

Specification
ECMAScript 2027 LanguageSpecification
# sec-object.prototype.hasownproperty


Web Proxy Viewer  |  New URL  |  Original Page