|
Replies:
1
-
Pages:
1
-
Last Post:
Nov 21, 2005 6:39 AM
by: davecrane
|
|
|
Posts:
5
Registered:
11/18/05
|
|
|
|
Appendix B Errata
Posted:
Nov 18, 2005 2:12 PM
|
|
In B.2.2 Constructor functions, classes, and prototypes. p. 599, 1st. paragraph, 2nd sentence
It states "..., and objects will inherit from the prototype only what has already been added to it before the constructor is invoked."
along with an example,
However in testing, and in David Flanagan's The Definitive Guide. Fourth Edition (Jan 2002) 11/02 printing.
In Section: 8.4 Prototypes and Inheritance on p. 120. Fifth paragraph, last sentence.
"The second implication is that an object inherits properties even if they are added to its prototype after the object is created."
I used the following code modeled on the example to test.
function MyObject() { this.test = "test"; }
MyObject.prototype.color="red"; var obj1=new MyObject(); MyObject.prototype.color="blue"; MyObject.prototype.soundEffect = "boOOOoing!"; var obj2=new MyObject(); alert(obj1.color + " " + obj2.color); alert(obj1.soundEffect + " " + obj2.soundEffect);
And, it alerts with "blue blue", and "boOOOoing! boOOOoing!" in both Mozilla 1.7.12 and Internet Explorer 6.0.2900.2180.xpsp_sp2_gdr.050301-1519.
|
|
Posts:
149
From:
Stroud, UK
Registered:
11/9/04
|
|
|
|
Re: Appendix B Errata
Posted:
Nov 21, 2005 6:39 AM
in response to:
BrianAndrewSmith
|
|
Hi Brian,
You're absolutely right - shame on me for relying on memory in that case.
I remember getting into some trouble with objects of the same type getting diverging properties, way back in my javascript-on-set-top-boxes days in 1999. Whether the JS implementation was non-standard, or I was actually modifying properties rather than the prototype, I do not know now, but for whatever reason, I've been carrying that mistake around with me for six years.
In any event, the big take home message from that section is the following sentence 'there is little value in altering prototypes on the fly', and I'd stick with that guiding principle. (Indeed, I've stuck by it so well myself, that I was able to make that mistake in the first place.)
Thanks for pointing this out, and hope you enjoy the rest of the book.
Regards,
Dave
|
|
|
Legend
|
|
Gold: 300
+
pts
|
|
Silver: 100
- 299
pts
|
|
Bronze: 25
- 99
pts
|
|
Manning Author
|
|
Manning Staff
|
|