var course = new Object();
var course = {
title: "JavaScript Essential Training",
instructor: "Morten Rand-Hendriksen",
level: 1,
published: true,
views: 0,
updateViews: function() {
return ++course.views;
},
};
console.log(course);
console.log(course);
Asked
Active
Viewed 29 times
-3

mplungjan
- 169,008
- 28
- 173
- 236

Ayush Pattnaik
- 31
- 1
- 6
3 Answers
1
You are assigning an empty object to the variable course.
var course = new Object();
Now you are assigning an object with properties into it.
var course = {
title: "JavaScript Essential Training",
instructor: "Morten Rand-Hendriksen",
level: 1,
published: true,
views: 0,
updateViews: function() {
return ++course.views;
},
};
Here an object is assigned to variable course and updateViews method will not call as this is an initialization. So if you want to update views you should call updateViews from your variable course. Like this
console.log(course.updateViews());

Aman Kumayu
- 381
- 1
- 9
0
It would help if you could post what you actually see. However, surely you want to call updateViews() to change the views. Just asking it to write out the current value of the object does not call each method on the object.

Simon
- 736
- 6
- 21
-
sorry, i am new – Ayush Pattnaik May 16 '20 at 19:30
-
no worries, we all started somewhere - just put as much info as you can so people can assist without needing to ask :) welcome! – Simon May 16 '20 at 19:41
0
Just console.logging the object does not update the views.
Also your first declaration is not useful.
If you want you could do this:
var course = {
title: "JavaScript Essential Training",
instructor: "Morten Rand-Hendriksen",
level: 1,
published: true,
views: 0,
updateViews: function() {
return ++course.views;
},
toString : function() { this.updateViews(); return this }
};
console.log(course.toString());
console.log(course.toString());
I found a more detailed answer

mplungjan
- 169,008
- 28
- 173
- 236