473,406 Members | 2,549 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,406 software developers and data experts.

question about Crockford essay - assigning methods to an object's prototype

I'm reading an essay, I think one of Crockford's, and it has this
example in it:

function Demo() { }
Demo.prototype = new Ancestor();
Demo.prototype.foo = function () { } ;

Does Ancestor now have a function called foo?

What if I have 5 different objects, all descended from Ancestor? Do
they all now acquire a method named foo, if I just once assign a method
like this?

Nov 8 '05 #1
4 1740
lk******@geocities.com wrote:
function Demo() { }
Demo.prototype = new Ancestor();
Demo.prototype.foo = function () { } ;

Does Ancestor now have a function called foo?
No, objects derived from Demo have. Which can be easily tested with

alert(Ancestor.foo);
alert(Ancestor.prototype.foo);
alert(Demo.foo);
alert(Demo.prototype.foo);
alert(new Ancestor().foo);
alert(new Demo().foo);

and the like.
What if I have 5 different objects, all descended from Ancestor?
Do they all now acquire a method named foo, if I just once assign
a method like this?


No. Assigning (rather: defining) a method like this affects the modified
prototype (Demo) and objects derived from it, not its prototype (Ancestor)
or objects derived from that.
PointedEars
Nov 8 '05 #2

Thomas 'PointedEars' Lahn wrote:
lk******@geocities.com wrote:
function Demo() { }
Demo.prototype = new Ancestor();
Demo.prototype.foo = function () { } ;

Does Ancestor now have a function called foo?


No, objects derived from Demo have. Which can be easily tested


Sorry for being stupid, but how is this:

Demo.prototype.foo = function () { } ;

different from this:

Demo.foo = function () { } ;

In both cases Demo ends up with a method called foo, yes?

I guess I don't understand the language but this line:

Demo.prototype = new Ancestor();

Looks like it's saying "Demo's prototype is Ancestor."

And this line:

Demo.prototype.foo = function () { } ;

looks like its saying "We will now assign a function foo to the the
prototype of Demo, which is Ancestor."

Nov 8 '05 #3
lk******@geocities.com writes:
Sorry for being stupid, but how is this:

Demo.prototype.foo = function () { } ;

different from this:

Demo.foo = function () { } ;

In both cases Demo ends up with a method called foo, yes?
No.

Demo is a function, used as a constructor function, and therefore
an object (all functions are objects and can have properties).

In the latter case, Demo itself has a method (a function property)
called "foo".

In the former, Demo has no property called foo, but the object
referenced by Demo.prototype does. That means that all objects created
by "new Demo()" will have an method called "foo", inherited from its
prototype.
I guess I don't understand the language but this line:

Demo.prototype = new Ancestor();

Looks like it's saying "Demo's prototype is Ancestor."
It reads like that, but it's not exactly what it means.

You are confusing the function "Ancestor" and the (non-function)
objects created from it using "new Ancestor()".

Constructor functions, like Demo and Ancestor here, are merely a
way to perform prototype inheritance between objects.

Each object in Javascript has another object as its prototype [1], and
it inherits all properties of that prototype object that isn't hidden
by a property on the object itself. Since the prototype itself is an
object and has a prototype, we talk about the prototype *chain* of an
object.

When writing "new SomeFunction()", what happens is that a new object
is created, and its prototype object is set to the object referenced
by SomeFunction.prototype at that time. After that, the body of
SomeFunction is executed with "this" referring to the new object.
After this, SomeFunction has nothing more to do with the new object,
it is just used for initialization.

So "Demo.prototype = new Ancestor()" really means:
Create a new object inheriting from Ancestor.prototype and
initialized by running Ancestor (and then we can forget about
Ancestor).
Then set that object as the prototype of objects created using "new
Demo()".
And this line:

Demo.prototype.foo = function () { } ;

looks like its saying "We will now assign a function foo to the the
prototype of Demo, which is Ancestor."


We will now assign a function to the prototype of objects created from
Demo (by "new Demo()"), which was originally created using Ancestor.
Now for the fun. Try running this and understand the results :)
---
function Foo(){};
function Bar(){};
var foo = new Foo();
var bar = new Bar();
Foo.prototype.baz = 42;
alert(foo.baz); // alerts 42! the prototype link is live
Bar.prototype = Foo.prototype;
alert(bar.baz); // alerts undefined. Inheritance is between objects,
// the function is not important afterwards.
alert(foo instanceof Bar); // alerts true - really tests if
// Bar.prototype is in foo's prototype chain.
// Again, the function itself is not important
---

/L
[1] Ok, there *is* one "root" object that doesn't have a prototype:
It's available as Object.prototype.
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Nov 9 '05 #4
On 08/11/2005 22:50, lk******@geocities.com wrote:

[snip]
Sorry for being stupid, but how is this:

Demo.prototype.foo = function () { } ;

different from this:

Demo.foo = function () { } ;

In both cases Demo ends up with a method called foo, yes?
Lasse described the difference with regard to inheritance. With the
latter, the Demo constructor has a property, foo, but instances will not:

function Demo() {}
Demo.foo = function() {};

var myInstance = new Demo();

Demo.foo(); /* OK */
myInstance.foo(); /* Run-time error: foo is not a function */

Another important consideration is that with the former, all instances
of Demo will share the /same/ foo method:

/* Returns a function object with the
* variable, calls, in its scope chain.
*
* Multiple calls to createMethod will
* create new function objects, each
* with their own variable, calls.
*/
function createMethod() {
var calls = 0;

return function() {
return ++calls;
};
}
function Demo() {}
Demo.prototype.foo = createMethod();

var myInstance1 = new Demo(),
myInstance2 = new Demo();

myInstance1.foo(); /* 1 */
myInstance1.foo(); /* 2 */

myInstance2.foo(); /* 3 (Even though it's a
* different instance!)
*/

Compare that to a method added within the constructor function:

function Demo() {
this.foo = createMethod();
}

var myInstance1 = new Demo(),
myInstance2 = new Demo();

myInstance1.foo(); /* 1 */
myInstance1.foo(); /* 2 */

myInstance2.foo(); /* 1 */
I guess I don't understand the language but this line:

Demo.prototype = new Ancestor();

Looks like it's saying "Demo's prototype is Ancestor."
An instance of Ancestor.
And this line:

Demo.prototype.foo = function () { } ;

looks like its saying "We will now assign a function foo to the the
prototype of Demo, which is Ancestor."


Again, "...which is an instance of Ancestor." The Demo prototype object
is a separate, unique instance. /It/ will be changed, but Ancestor will not.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Nov 9 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joe Kelsey | last post by:
When defining public methods, which style do you prefer: style 1: function myObject (...) { this.member = 0; } myObject.prototype.method = function ()
2
by: Richard Trahan | last post by:
In the 1.5 Guide, Chapter 8 (Details of the Object Model), last sentence: "The dennis object does not inherit this new property". I don't understand why this is so. A few pages earlier, it says...
4
by: Christopher Benson-Manica | last post by:
Is this code both correct and canonical? function foo( arg ) { this.initialize( arg ); } foo.prototype.initialize=function( arg ) { // Initialize based on arg }
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
2
by: Peter Michaux | last post by:
Douglas Crockford doesn't seem to like JavaScript's built-in syntax for building new objects based on a prototype object. The constructor function, its prototype property and the "new" keyword all...
7
by: RubyRedRick | last post by:
I bought Crockford's "JavaScript: The Good Parts" yesterday to help build my JavaScript foo. On page 44, he gives an implementation of the curry function: Function.method('curry', function() {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.