0

As MDN says, the for...in statement has access to all properties (and values) related to an object.

I am not sure about why are methods also listed with this statement: if I loop on the document object I get not only the properties list related to it, but also the methods such as prompt() or focus()

Why are these methods named as "properties" on all documentation related to the for...in loop?

3
  • 4
    Because methods are properties? Commented Jul 28, 2016 at 11:55
  • Because in javascript an object is a collection of name : value pairs and the value can be a function, this is how you get a method. I do not think that for...in is meant to be used on things like document Commented Jul 28, 2016 at 11:57
  • 3
    @GerardoFurtado a better question is, why are prompt and focus enumerable? Commented Jul 28, 2016 at 11:59

2 Answers 2

2

Because methods are also properties, just with the type of function.

See for yourself:

for (var prop in document) { console.log("document." + prop + " = " + document[prop], typeof(document[prop])); } 
Sign up to request clarification or add additional context in comments.

Comments

1

Please check this link, i think it will be helpful:https://msdn.microsoft.com/en-us/library/ms229054(v=vs.100).aspx...

1 Comment

Thank you. I'll take a deep look at this :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.