Connecting Tech Pros Worldwide Forums | Help | Site Map

Advantage of using prototype objects over regular functions?

Yansky
Guest
 
Posts: n/a
#1: Feb 2 '08
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 seems like it does
the same thing as regular functions except in a more complicated way.



Steve Swift
Guest
 
Posts: n/a
#2: Feb 2 '08

re: Advantage of using prototype objects over regular functions?


Yansky wrote:
Quote:
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 seems like it does
the same thing as regular functions except in a more complicated way.
The general idea (it seems to me) behind these JavaScript libraries is
that you only have to learn how to do things once. If you try to do
things in native JavaScript you end up learning how to do it in Internet
Explorer, then Firefox, then Opera, then Sarfari, then …

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk
Joost Diepenmaat
Guest
 
Posts: n/a
#3: Feb 2 '08

re: Advantage of using prototype objects over regular functions?


Steve Swift <Steve.J.Swift@gmail.comwrites:
Quote:
Yansky wrote:
Quote:
>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 seems like it does
>the same thing as regular functions except in a more complicated way.
>
The general idea (it seems to me) behind these JavaScript libraries is
that you only have to learn how to do things once. If you try to do
things in native JavaScript you end up learning how to do it in
Internet Explorer, then Firefox, then Opera, then Sarfari, then …
I don't think the poster was talking about the Prototype.js library.

Then again, I could be wrong.

Joost.
Joost Diepenmaat
Guest
 
Posts: n/a
#4: Feb 2 '08

re: Advantage of using prototype objects over regular functions?


Thomas 'PointedEars' Lahn <PointedEars@web.dewrites:
Quote:
Yansky wrote:
Quote:
>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.
Joost Diepenmaat
Guest
 
Posts: n/a
#5: Feb 3 '08

re: Advantage of using prototype objects over regular functions?


Thomas 'PointedEars' Lahn <PointedEars@web.dewrites:
Quote:
Joost Diepenmaat wrote:
Quote:
>Thomas 'PointedEars' Lahn <PointedEars@web.dewrites:
Quote:
>>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.
>
Nonsense. As every function is a method and `null' has no properties, the
`this' value is never `null'.
Oops, I forgot about the rule that said that if this is null or some
other non-object, it becomes the global object.

joost.

VK
Guest
 
Posts: n/a
#6: Feb 3 '08

re: Advantage of using prototype objects over regular functions?


On Feb 3, 6:16 pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
Quote:
Oops, I forgot about the rule that said that if this is null or some
other non-object, it becomes the global object.
Still not exactly right. null as argument is used in call() and
apply() methods to enforce the global context for callee. This way
"this" is never null, but null argument is used in some methods to get
a particular "this" state.

uncle_billy
Guest
 
Posts: n/a
#7: Feb 12 '08

re: Advantage of using prototype objects over regular functions?


On Feb 2, 2:45 am, Yansky <thegoodd...@gmail.comwrote:
Quote:
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 seems like it does
the same thing as regular functions except in a more complicated way.
I think what the orginal poster was trying to ask is something like
this: what are the advantages to using one or the other of these?


var Car = function() {
this.wheels = 4;
this.moveForward = function() {
...
}
}

var Car = function() {
this.wheels = 4;
}
Car.prototype.moveForward = function() {
...
}
Joost Diepenmaat
Guest
 
Posts: n/a
#8: Feb 12 '08

re: Advantage of using prototype objects over regular functions?


Thomas 'PointedEars' Lahn <PointedEars@web.dewrites:
Quote:
You would gain at least some memory efficiency for losing at least some
runtime efficiency, because you would only need one Function object for all
Car objects but it would have to be looked up through the prototype chain on
each property access.
True. But note that the runtime property-lookup penalty will be offset
by having more efficient object creators. In other words, creating a lot
of objects with a lot of "interited" properties will be faster, and if
you only access a subset of all properties of all objects the total run
time /may/ be faster with prototype inheritance than without.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Closed Thread