Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
Yansky wrote:
>I'm just starting to learn about the prototype object in javascript
and I was wondering if someone could explain just in laymans terms why
you would use it instead of a regular functions?
It only seems as if there are regular functions. Globally declared
functions are in fact methods (callable properties) of the Global Object.
Locally declared functions are methods of the local Variable Object (that
can not be referred to).
I prefer to state it more or less the the other way around; all
functions can be called as methods and as functions. A method call sets
the this object to something useful, a function call sets the this
object to null. he caller determines which of the two is used.
Poperty-lookup (if used) is responsible for the inheritance mechanism
which is independent of the actual calling. *
I find this a useful and practical view as a programmer. I'm not
claiming that this is exactly what happens in any implementation.
* obj.prop(arg) is just syntactic sugar for obj.prop.call(obj,arg) (and
yes, that's a circular definition, but JS is full of those edge cases).
Joost.