I am new to JavaScript, and I am now confused by the two conceptions: object with constructor and prototype, and closure.
Here, closure means function with other functions nested in it, and its environment.
I feel that these two conceptions seems to be similar from a certain point of view:
they both have variables, which is like fields in other languages. In closure, it is called environment, which I think is a kind of binding between values and certain variables.
Moreover, they both have “methods”. In closure, I think the inner nested functions act just like this.
So, what is the essential difference between these two conceptions? Or, how the two approaches to remembering data differ?
If I made some errors in these conceptions above, please correct me, thank you.
Well, a closure doesn’t have ‘methods’ as such, as it isn’t really a concrete object – its more of a concept. Closures are implimented using functions, and functions themselves can have methods, but you wouldn’t describe a closure as having methods. As you say, a closure is a function plus information about its environment.
On the other hand a prototype is just a basic JavaScript object, so its correct to say that a prototype has methods.