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
for(var foo in document)
gives megetElementById
(among many others),for(var foo in Object.getPrototypeOf(document))
also seems to give me the same values andObject.getPrototypeOf(document)
returnsHTMLDocument
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))