Raina
Raina

Reputation: 99

Unable to view the contents of the object in JavaScript

Trying to execute the following piece of code -

 <script>
      var Person = function(name, age, email) {
        this.name = name;
        this.age = age;
        this.email = email;
      };

      var person = new Person(“fred”, 30, “[email protected]”);
      for (var prop in person)
        console.log(prop + “ = “+person[prop]);
    </script>

Expected Output:

name = fred
age = 30
email = [email protected]

Link to the fiddle - https://jsfiddle.net/raina140291/mw2c704d/

I already installed the external script to view the contents of the console, but it doesn't display anything for the above code.

Fairly new to java script, would appreciate some guidance.

Upvotes: 0

Views: 35

Answers (2)

Rajshekar Reddy
Rajshekar Reddy

Reputation: 18987

Your only issue was with this “fred”, 30, “[email protected] its not same as "fred", 30, "[email protected]" or 'fred', 30, '[email protected]'. Change it and everything seems fine.

var Person = function(name, age, email) {
    this.name = name;
    this.age = age;
    this.email = email;
  };

  var person = new Person('fred', 30, '[email protected]');
  for (var prop in person)
    document.getElementById('log').innerHTML += prop + ' = '+person[prop] + '<br/>';
<pre id="log"></pre>

Upvotes: 1

Tom
Tom

Reputation: 8180

You must have copy and pasted this from somewhere as the only problem is that you have the wrong kind of quotes. Compare:

“fred”

should be

"fred" or 'fred'

Upvotes: 2

Related Questions