0

I try to understand why this works

// .......................................................

var hero = { 
  name: 'Joni', 
  type: 'blond',
  sayName: function() { 
    return this.name; 
  } 

  sayType: function() { 
    return this.type; } 
} 
document.write( hero.sayName()+ "</br>"); 
document.write( hero.sayType());

// .......................................................

but this doesn't works

// .......................................................

var hero = { 
  name: 'Joni', 
  type: 'blond',
  sayName: function() { 
    return this.name; 
  } 

  sayType: function() { 
    return this.type; 
    } 
} 
document.write( hero.sayName()+ "</br>"); 
document.write( hero.sayType());

// .......................................................

thanks

6
  • What doesn't work exactly, and where is the difference between the two code blocks? Commented Feb 22, 2011 at 10:07
  • 3
    Kill me, but where is the difference? Commented Feb 22, 2011 at 10:08
  • 3
    it shouldn't work on both because of a missing comma. Commented Feb 22, 2011 at 10:09
  • 1
    also, there's a comma missing after the sayName property. Commented Feb 22, 2011 at 10:09
  • Please post your actual code. Both examples contain errors and should not work. Commented Feb 22, 2011 at 10:11

3 Answers 3

1

You are missing a semi-colon at the end of the "var hero" statement. You are also missing some other commas.

var hero = { 
  name: 'Joni', 
  type: 'blond',
  sayName: function() { 
    return this.name; 
  }, // <<--- You missed the comma here

  sayType: function() { 
    return this.type; 
    } 
};  // <<--- You missed the semi colon!
document.write( hero.sayName()+ "</br>"); 
document.write( hero.sayType());

Going forward, you can completely avoid these issues by just running your code through JSLINT. Goto jslint.com, paste your code, and you'll see the answers revealed to you.//

Sign up to request clarification or add additional context in comments.

3 Comments

The semi colon isn't required in this instance: stackoverflow.com/questions/42247/…
@aiham. It might not be "required", but you are relying on javascript's semi-colon insertion. Which, if you read Crawford's book, you'll learn is a bad thing and can't always be relied on. On my team, you wouldn't be allowed to check this in.
I wouldn't do it either, but it's not what was giving the error.
0

The difference is in the enter before the saytype function in the second block. Besides the comma i see no reason why it wouldn't work.

Comments

0

You are missing a comma after the sayName function. I couldn't see a difference between the two blocks of code, other than a new line character (Which doesn't make a difference). Both blocks of code were missing this comma.

var hero = { 
  name: 'Joni', 
  type: 'blond',
  sayName: function() { 
    return this.name; 
  },// <---  Missing Comma

  sayType: function() { 
    return this.type; 
    } 
} 
document.write( hero.sayName()+ "</br>"); 
document.write( hero.sayType());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.