Subject: Functions
Posted By: LTello Post Date: 10/14/2005 9:03:59 AM
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
Reply By: nzakas Reply Date: 10/15/2005 12:53:56 PM
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:


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/
Reply By: LTello Reply Date: 10/16/2005 3:17:57 PM
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

Reply By: JBucci Reply Date: 9/15/2006 11:15:19 AM
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



Go to topic 49817

Return to index page 174
Return to index page 173
Return to index page 172
Return to index page 171
Return to index page 170
Return to index page 169
Return to index page 168
Return to index page 167
Return to index page 166
Return to index page 165