0
>>function Foo(){this.a = "a"}
>>var foo = new Foo()

>>Object.getOwnPropertyDescriptors(foo)
a: {value: "a", writable: true, enumerable: true, configurable: true}
__proto__: Object

>>foo.hasOwnProperty("__proto__")
false

I understand line 8's result.

But I think there is some information stored in the foo that tells JS Engine which function create foo.

Maybe foo has a hidden property tell JS Engine Foo creates foo, or maybe there is a list stored in the memory that record Foo creates foo.

What's the fact? How does JS Engine know foo is an instance of Foo but no Array?

Shuai Li
  • 2,426
  • 4
  • 24
  • 43
  • Well `Object.getPrototypeOf(foo).constructor == Foo`, but other than that why do you think the JS engine would be interested in which function created the instance? – Bergi Feb 26 '18 at 11:04
  • What has `arr.hasOwnProperty("__proto__")` to do with this, there's no `arr` in your code? Does't that relate to [your previous question](https://stackoverflow.com/q/48984407/1048572)? – Bergi Feb 26 '18 at 11:05
  • @Bergi sorry,it is Opps – Shuai Li Feb 26 '18 at 11:08
  • Still I thought I already had answered why `__proto__` is not found as an own property, it's the same for `foo` as it was for `arr` in the previous question. So what else do you need to know? – Bergi Feb 26 '18 at 11:10
  • As I know,`foo` only has one property`a`, foo does not have `__proto__`or `constructor`.when I try to use foo.toString() or other methods, how does JS Engine find those methods? – Shuai Li Feb 26 '18 at 11:15
  • Because they are inherited! Do you understand how [prototype inheritance](https://en.wikipedia.org/wiki/Prototype-based_programming) works? – Bergi Feb 26 '18 at 11:18
  • @Bergi Back to the first question:how does JS Engine calculates `foo.__proto__=== Foo.prototype`? `foo` does not have a property which named `__proto__` – Shuai Li Feb 26 '18 at 11:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165819/discussion-between-wind-west-and-bergi). – Shuai Li Feb 26 '18 at 11:26
  • Every object has a hidden internal property containing the data that `__proto__` reports;`__proto__` itself happens to be an inherited accessor property, but every object "knows" what its prototype is. – jmrk Feb 26 '18 at 18:36

1 Answers1

0

foo.constructor will tell you what was the constructor function that was used to create the object

Jiby Jose
  • 3,745
  • 4
  • 24
  • 32