0

I am trying to get a better working knowledge of JavaScript. So, I have bought the book, "JavaScript the good parts" by Douglas Crockford.

I am having difficulty grasping the Prototype at the moment. Everything below seems to work in my browser until I hit //PROTOTYPE Example. Can someone have a look at it to see why I cant get any output from it. (My page returns blank unless I comment all of the prototype code out)

Thank you for any assistance.

Barry

var stooge = { 
 "first-name": "Jerome",
 "last-name": "Howard",
 "nickname": "J", 
 "profession" : 'Actor' 
};
// below is augmenting
var st = stooge;
st.nickname = "curly";
// st.nickname and nick are the same because both are ref's to the same object 
var nick = st.nickname;
document.writeln(stooge['first-name']); //expect Jerome -- this is "suffix" retrieval 
document.writeln(st.nickname); //expect "curly" -- this is "notation" retrieval
document.writeln(nick); //expect "curly"
document.writeln(stooge.profession); 
//PROTOTYPE EXAMPLE; 
if (typeof Object.create !== 'function')
{
 object.create = function(o) {
 var F = function () {}; 
 F.prototype = o; 
 return new F();
};
var another_stooge = Object.create(stooge);
another_stooge['first-name'] = 'Barry'; 
document.writeln(another_stooge['first-name']);
// the below should be inherited from the prototype therefore "Actor" 
document.writeln(another_stooge.profession);
asked Oct 27, 2009 at 11:18

2 Answers 2

5

You're missing a closing brace at the end of the function expression assigned to object.create, and also you haven't capitalized Object in object.create = function(o) {.

//PROTOTYPE EXAMPLE; 
if (typeof Object.create !== 'function')
{
 Object.create = function(o) { // <--- "Object" instead of "object"
 var F = function () {}; 
 F.prototype = o; 
 return new F();
 };
} // <--- Closing brace was missing
Ferdinand Beyer
67.7k18 gold badges161 silver badges147 bronze badges
answered Oct 27, 2009 at 11:23
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Tim, I wont get caught out by that again now :)
3

You seem to be missing the closing brace for the line object.create = function(o) {.... I see a closing brace for the if-statement and for the var F = function () {};, but not for function(o).

A missing closing brace would indeed suppress output, because Javascript would assume everything before the (missing) closing brace is part of a function definition, not something to be executed (yet).

answered Oct 27, 2009 at 11:23

1 Comment

Thank you for this. Both answers solve my problem exactly. I will mark Tim as answer as he has less points and also pointed out the capitalization of O, but thank you very much Mark you helped my understanding :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.