0

Why is it that when I add a function to an object it is being part of Object.keys(myObj) while this is not the case for e.g. document.getElementById and Object.keys(document)

var myObj = {}
myObj.text = "hi"
myObj.func = function(){}

console.log(Object.keys(myObj))
console.log(Object.keys(document))

console.log(typeof myObj)
console.log(typeof document)
console.log(typeof myObj.func)
console.log(typeof document.getElementById)

Note that while

  1. for(var foo in document) gives me getElementById (among many others),
  2. for(var foo in Object.getPrototypeOf(document)) also seems to give me the same values and
  3. Object.getPrototypeOf(document) returns HTMLDocument

for(var foo in HTMLDocument) just gives me what seems to be Object.keys(Node) (Object.getPrototypeOf(HTMLDocument) is Node):

var resArr = []
for(var foo in document) resArr.push(foo)
console.log("results (var foo in document): " + resArr.length)
var resArr = []
for(var foo in Object.getPrototypeOf(document)) resArr.push(foo)
console.log("results (var foo in Object.getPrototypeOf(document)): " + resArr.length)
var resArr = []
for(var foo in HTMLDocument) resArr.push(foo)
console.log("results (var foo in HTMLDocument): " + resArr.length)
console.log("Object.getPrototypeOf(document): " + Object.getPrototypeOf(document))
console.log("Object.getPrototypeOf(document) === HTMLDocument: " + (Object.getPrototypeOf(document) === HTMLDocument))
console.log("Object.getPrototypeOf(document) == HTMLDocument: " + (Object.getPrototypeOf(document) == HTMLDocument))
console.log("Object.getPrototypeOf(document) instanceof HTMLDocument: " + (Object.getPrototypeOf(document) instanceof HTMLDocument))

Also note that contrary to what Bergi suggests by marking this as duplicate of 22658488 Object.getOwnPropertyNames(document) doesn't return those additional members (as getElementById) either:

console.log(Object.getOwnPropertyNames(document))
Community
  • 1
  • 1
sqln00b
  • 391
  • 3
  • 5
  • 2
    Because `getElementById` is not directly defined on `document` object but on one of its prototype chain i.e. `Document` . – abhishekkannojia Mar 23 '17 at 13:41
  • Thanks! Still I don't have `getElementById` in `Object.keys(HTMLDocument.prototype)` (only `["fgColor", "linkColor", "vlinkColor", "alinkColor", "bgColor", "all", "clear", "captureEvents", "releaseEvents"]`) – sqln00b Mar 23 '17 at 13:47
  • See the edited comment, it is defined on `Document ` object. Also use `Object.getPrototypeOf(HTMLDocument)` to get the prototype of Object. There is difference between the two. – abhishekkannojia Mar 23 '17 at 13:55
  • Ok, what I wanted is probably the list of 244 entries the [for…in loop](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/for...in) gives me. Is there a functional equivalent? Apparently neither `Object.keys()` nor `Object.getOwnPropertyNames()` are, right? – sqln00b Mar 23 '17 at 13:56
  • As far as I know, there is no `Object.keys` equivalent function which enumerates the object's prototype properties. You'll have to use `for...in` loop. – abhishekkannojia Mar 23 '17 at 14:02
  • The `for…in` loops of `Document`, `HTMLDocument` and `Node` all seem to give me what is defined in `Node` (`Object.keys(Node)`) (and doesn't include `getElementById` and the many other properties `for (var foo in document)` gives me). Also `Object.getPrototypeOf` tells me that `Node` is the `prototype` of both `Document` and `HTMLDocument`, while the `prototype` of `document` is shown to be `HTMLDocument` (not `Document`). Still i don't know where all the rest comes from – sqln00b Mar 23 '17 at 14:08
  • [`Object.entries()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) might be the closest to `for…in` allthough it doesn't iterate through the prototype chain – sqln00b Mar 23 '17 at 20:59

0 Answers0