Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a class and an object in Javascript

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.

like image 992
JamieJag Avatar asked Aug 10 '10 15:08

JamieJag


People also ask

What is the difference between class and object?

Class is a logical entity whereas objects are physical entities. A class is declared only once. On the other hand, we can create multiple objects of a class.

What is the difference between object and object in JavaScript?

Object() refers to the Object class constructor. So Object is a class that exist in JavaScript, while object was a variable you defined.

Is a JavaScript class just an object?

Classes are based on object-oriented programming concepts. As JavaScript is not an object-oriented programming language at its core, JavaScript classes are considered syntactic sugar.

What is difference between class and object constructor in JavaScript?

This function constructs objects, so it is a constructor. In JavaScript there are no classes. A class is a blueprint of an object. The difference between a class and an object is that a class is just a blueprint not a real physical object.


2 Answers

Javascript uses prototypal inheritance, so there are no classes per se. Everything is an object; it's just that some objects have a common parent object whose methods/variables will be found when name resolution looks up the prototype chain.

Your first code snippet creates an object called myView whose type is a function. Your second snippet defines an anonymous method which returns an object (with two properties, a and b) and then immediately calls this method, assigning the result to myView. So in this second case, myView is an object with two self-defined properties.

It may help you to read Douglas Crockford's description of prototypal inheritance in Javascript, as it sounds like you're a little fuzzy on the details.

like image 69
Andrzej Doyle Avatar answered Oct 31 '22 22:10

Andrzej Doyle


There are no classes in javascript.

As you mentioned, your first example would be for a re-usable object, whereas your second example is just for a singleton object.

The main difference here is that you're invoking that function immediately in the second example and it returns an object to you, whereas you need to explicitly invoke the first function each time using something like a=new myView() it's the () that's providing that invocation.

I use your 2nd example (known as crockford's module pattern) for one off page related tasks, and the first example for re-usable components within that page (some element generated many times with handlers etc)

Also read about protoypal inheritance so you can understand how to effectively use the first example for writing better performing javascript code.

like image 30
brad Avatar answered Oct 31 '22 22:10

brad