473,327 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Advantage of using prototype objects over regular functions?

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
7 1812
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
8
by: Elf M. Sternberg | last post by:
One of the complaints about prototype.js (google for it if you're not familiar with it) is that it's poorly documented. I have this inkling that the key to understanding prototype.js is in the...
8
by: Titatoutati | last post by:
Hi all, So, what I am trying do to is to manage some classes with javascript. It's working, but there are some things that I don't understand... Next is my sample code, and I have indicated the...
6
by: mmcloughlin | last post by:
I'm learning about objects and am trying to figure out how basic inheritance works. I've got into the habit of explicitly setting the prototype object with an object literal as it seems to make the...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
2
by: blackholebutterfly | last post by:
Why isn't this allowed? function Foo(){ this.param1 = 'Constructor Function Foo'; } var foo = new Foo; var newfoo = new foo; The last line above gives an error. I thought all objects...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
1
by: p.tilhoo | last post by:
Hello there, I am a programmer mostly using c# and just started using Javascript. I am trying to use prototype to declare and use an object and having difficulties in coding for nested...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.