PDA

View Full Version : Problems with JavaScript Object literal Notation



jp504
19 Mar 2011, 04:16 PM
Hello. I am a beginner at JavaScript. I am currently practicing JavaScript OOP, and can't get object literal notation to work. I am basically copying and pasting the script, but it still won't work.

Here is a small snippet of code that I can't get to work.

<script type="text/javascript">
var Person = {
name : "Jacob Michael Perkins";
show : function()
{
alert("Your name is " + this.name);
}
}

Person.show();
</script>


What the heck am I doing wrong. OOP using constructor functions work fine, but this won't work at all. Please help. Thanks in advance

nnelson04
21 Mar 2011, 10:41 AM
You've almost got it, just replace

name : "Jacob Michael Perkins";

with


name : "Jacob Michael Perkins",

and it'll work like a charm.