2

If every object inherits from Object why does console.log(e.hasOwn(test1.prototype,"eu")) give me this error?

Uncaught TypeError: e.hasOwn is not a function

function test1() {
  this.senha = "ooopa";
}

test1.word = "any"
test1.prototype.eu = "true"

function test2() {
  this.name = "euler";
}

var t = new test2();
var e = new test1();

console.log(Object.hasOwn(test1.prototype, "eu"));
console.log(e.hasOwn(test1.prototype, "eu"));
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 1
    `hasOwn` is not an *instance* method. It's a static method on `Object` itself. – kelsny Oct 10 '22 at 14:05
  • Every object inherits from the `Object.prototype` object; `hasOwn` is NOT defined in the `Object.prototype` object. – Yousaf Oct 10 '22 at 14:11

1 Answers1

2

Object.hasOwn() is a static method. Static methods are directly called on the class.

In this case it will be directly called on Object.

Other examples are Object.keys(),Object.entries().

Read

JS has prototype inheritance. Properties are looked up using the prototype chain. If a property is attached to the prototype then it will be available to the instances. Example: Object.prototype.toString()

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39