Jake Jake - 1 year ago 73
Javascript Question

Define variables local to a JavaScript class

How do we define variables specific to the scope of JavaScript class?

In this fiddle below, I would like to define a variable called

specific to the class
. I am getting an error
SyntaxError: missing : after property id

var Person = {
var name = "Jake";
printName: function()


Answer Source

You are creating Person wrongly and val() is not a javascript method. Try like following.

var Person = {
  name: "Jake",
  printName: function() {
    document.getElementById("personName").value = this.name;

<input type="text" id="personName">

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download