Difference between a class and an object in Javascript

Viewed 14030

What's the difference between

var myView = function () {
//something goes here
};

and

var myView = function () {
//something goes here
    return {
        a: x,
        b: y
}();

I think the first snippet creates a "dynamic" class, so that you can say

var anotherView = new myView();

and the second snippet is similar to a singleton "dynamic" object, but I'm not very sure.

3 Answers

var myView = function () { //something goes here };

This is function expression without being executed. And var myView = function () { //something goes here return { a: x, b: y }(); This function expression gets executed due to parenthesis "()" place after function resulting in return of Object.

Again New keyword use for creating constructor and can not be applicable for Object.

Related