Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0
This is the forum to discuss the Wrox book Professional JavaScript for Web Developers by Nicholas C. Zakas; ISBN: 9780764579080
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 14th, 2005, 09:03 AM
Registered User
 
Join Date: Oct 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Functions

Hi Nicholas,

     I wanted to know what is the difference between these two scripts of code.

Object.extend = function(){
...
}

Object.prototype.extend = function(){
...
}

I understand that the second one is creating a function called extend which is extended from class Object via prototype. If this is the case, why is the prototype lacking from the first function?

I wrote some code to understand a bit better:

Object.extend = function(){
    alert("f1");
}

Object.prototype.extend = function(){
    alert("f2");
}

var test = new Object();
extend(); /* f2 */
test.extend(); /* f2 */
Object.extend(); /* f1 */

I think that in the extend() case there is already a Object made. So if you put a this in front of extend it will work. In the second case, I created a new Object and I called extend() through the reference "test". Finally, I have Object.extend(), which displays f1 but I am confused if it is rewriting the function extend or creating a new function extend. I just would like to know what I am calling when I write Object.extend().

Thanks in advance,

Luis
 
Old October 15th, 2005, 12:53 PM
nzakas's Avatar
Wrox Author
 
Join Date: Dec 2004
Location: Peabody, MA, USA.
Posts: 217
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Luis,

Let me clarify. Object.extend defines a static method of the Object class, which means that you can call it without having an instance Object. Object.prototype.extend defines an instance method, meaning that you must have an instance of Object to use it. Anytime you define something using prototype, you are saying "when an object of this type is created, make sure it has this."

Just calling extend() without either Object or test should cause an error (it did when I tried it). Object.extend() is a direct reference to the function describe as Object.extend (the static method). test.extend() is a direct reference to Object.prototype.extend (the instance method). You can prove this by running the following code:

Code:
Object.extend = function(){
    alert("f1");
}

Object.prototype.extend = function(){
    alert("f2");
}

var test = new Object();
test.extend();  /* f2 */
Object.extend(); /* f1 */

alert(test.extend == Object.prototype.extend); /* true */
alert(test.extend == Object.extend); /* false */
Nicholas C. Zakas
Author, Professional JavaScript for Web Developers (ISBN 0764579088)
http://www.nczonline.net/
 
Old October 16th, 2005, 03:17 PM
Registered User
 
Join Date: Oct 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot Nicholas. I spent this weekend in the libray reading up on prototype and methods which helped, b/c when I read your response it all made sense.

Luis

 
Old September 15th, 2006, 11:15 AM
Registered User
 
Join Date: Oct 2005
Location: West Orange, NJ, USA.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Nicholas,

I agree that Object.extend defines a static method of the Object class but Object.prototype.extend does not need an instance of Object (or its descendants) to use it.

Object.prototype.extend = function(){
    alert("f2");
}

var test = new Object();
test.extend(); /* f2 */
Object.extend(); /* f2 */
String.extend(); /* f2 */

alert(test.extend == Object.prototype.extend); /* true */
alert(test.extend == Object.extend); /* true */
alert(Object.extend == String.extend); /* true */
alert(test.extend == String.extend); /* true */

Where Object.extend would only be accessible via the Object class, Object.prototype.extend is also inherited by descendants of Object (Array, String, etc.) and is accessible via the class objects (Object, Array, String, etc.) or the instantiated objects of these classes.

Thanks,
Jerry






Similar Threads
Thread Thread Starter Forum Replies Last Post
C functions...??? ethantinder C# 2 April 25th, 2008 02:25 AM
Functions czambran BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 13 July 11th, 2006 06:33 PM
functions xelepi PHP How-To 1 March 10th, 2006 02:59 AM
Functions Meg Intro Programming 1 January 23rd, 2006 01:03 PM
Help with Functions megabytes C# 1 August 7th, 2003 12:48 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.