By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,191 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

Advantage of using prototype objects over regular functions?

P: n/a
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.
Feb 2 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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 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
Feb 2 '08 #2

P: n/a
Steve Swift <St***********@gmail.comwrites:
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 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.
Feb 2 '08 #3

P: n/a
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.
Feb 2 '08 #4

P: n/a
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
Joost Diepenmaat wrote:
>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.

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.

Feb 3 '08 #5

P: n/a
VK
On Feb 3, 6:16 pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
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.

Feb 3 '08 #6

P: n/a
On Feb 2, 2:45 am, Yansky <thegoodd...@gmail.comwrote:
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() {
...
}
Feb 12 '08 #7

P: n/a
Thomas 'PointedEars' Lahn <Po*********@web.dewrites:
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/
Feb 12 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.