Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Advantage of using prototype objects over regular functions?

Reply
Thread Tools

Advantage of using prototype objects over regular functions?

 
 
Yansky
Guest
Posts: n/a
 
      02-02-2008
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.
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      02-02-2008
Yansky <> writes:

> 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.


Are you asking about the advantages and disadvantages of object-oriented
constructs w/ method calls vs function calls, or about the use of
prototypes vs putting all properties including methods in the same
object?

Objects provide their own mechanism for property lookup. This means that
different objects can provide methods and properties with the same name
to do different things. In other words, they provide a straight forward
way of doing polymorphism:

myCollectionOfObjects.forEach(function(o) {
o.quack(); // what quack() does is determined by o itself
});

Functions are useful for actions that aren't intimately tied to a single
object and they're easier to use in higher-order constructs - getting
the above construct to work using "pure oo" only would get seriously
ugly very soon, see classic Java's sortable collections for instance.

As for using prototypes vs direct object properties; prototypes can be
shared so they're more efficient if you otherwise need to copy /
initialize a lot of properties. Also, they make it easy to extend
existing "classes of" objects:

this code from the mozilla developer site:

if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fun, /* thisp */) {
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this)
fun.call(thisp, this[i], i, this);
}
};
}

Joost.
 
Reply With Quote
 
 
 
 
Steve Swift
Guest
Posts: n/a
 
      02-02-2008
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
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      02-02-2008
Steve Swift <> writes:

> 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.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-02-2008
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).

> It seems like it does the same thing as regular functions except in a more
> complicated way.


Read on object-oriented programming to see the advantages of a reusable
encapsulation structure.

As for laymen terms, you can bite in every apple. Let bite() be a method of
people (with the target of biting as argument), or of apples (maybe with the
biting person or the size/form of the bit as argument).


HTH

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      02-02-2008
Thomas 'PointedEars' Lahn <> writes:

> 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.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-02-2008
Joost Diepenmaat wrote:
> Thomas 'PointedEars' Lahn <> writes:
>> 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'.


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$>
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      02-03-2008
Thomas 'PointedEars' Lahn <> writes:

> Joost Diepenmaat wrote:
>> Thomas 'PointedEars' Lahn <> writes:
>>> 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.

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      02-03-2008
On Feb 3, 6:16 pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
> 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.

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-03-2008
VK wrote:
> On Feb 3, 6:16 pm, Joost Diepenmaat <jo...@zeekat.nl> wrote:
>> 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.


Unlike Joost, you have not understood ECMAScript Ed. 3, section 10.2.3,
among several other sections and aspects of the programming language.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class prototype vs C function prototype June Lee C++ 2 04-13-2008 08:17 PM
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
Microsoft SQL , Sybase advantage SQL,Advantage database Tommy Computer Support 1 11-29-2007 04:21 AM
Prototype Object.extend(new Base() | Hash | Hash.prototype) usage: jacobstr@gmail.com Javascript 3 03-27-2007 07:56 AM
relation between prototype and Prototype.js shypen42@yahoo.fr Javascript 9 05-26-2006 01:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57