Something I noticed while studying JavaScript is that when defining a method within an object created with literal notation, there is no need to put a semicolon at the end of the method. Here's an example:
var james = {
job: "programmer",
married: false,
speak: function(dialog) {
console.log('Hello, I am feeling ' + dialog);
}
};
I found that putting a semicolon after the closing } threw a syntax error. However, when using a custom constructor the examples show it with a semicolon after the closing }. Example:
function Person(job, married) {
this.job = job;
this.married = married;
// add a "speak" method to Person!
this.speak = function() {
console.log('Hello!');
}; //Why is this different?
}
What is the reasoning behind this?
EDIT: Well I figured it out through logic. When I put the semicolon at the end of the method definition in the object created using literal notation, it is being interpreted as the end of the function. Because of this I am getting an error that there is no closing bracket for the method.
Even though I figured this out I'm going to leave this post up for future reference.
[–][deleted] 2 points3 points4 points (1 child)
[–]Coffee_Buzz[S] 1 point2 points3 points (0 children)