Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object vs Class vs Function

I was wondering - what's the difference between JavaScript objects, classes and functions? Am I right in thinking that classes and functions are types of objects?

And what distinguishes a class from a function? Or are they really the same thing, just the term for them changes according to how they are used?

function func() { alert('foo'); } // a function func(); // call the function - alerts 'foo' var func2 = function () { alert('hello'); } // acts the same way as 'func' surely? func2(); // alerts 'hello'  var Class = function() { alert('bar'); }; // a class var c = new Class(); // an istance of a class - alerts 'bar' 

Sure, classes have methods and properties and can be instantiated - but then, I could do the same with any old function - or not?

like image 470
Sean Bone Avatar asked Jul 08 '13 11:07

Sean Bone


People also ask

What is the difference between object and function?

An object is a collection of functions and data. A function is a collection of commands and data. When a bunch of functions work together to perform a certain task we may call this community of functionality an object.

Is class a function or object?

Object -- an encapsulation of data along with functions that act upon that data. Class -- a blueprint for objects. A class is a user-defined type that describes what a certain type of object will look like. A class description consists of a declaration and a definition.

What is class object and function?

A Class is a user defined data-type which has data members and member functions. Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions defines the properties and behavior of the objects in a Class.

What is the difference between class and function?

Functions do specific things, classes are specific things. Classes often have methods, which are functions that are associated with a particular class, and do things associated with the thing that the class is - but if all you want is to do something, a function is all you need.


1 Answers

As you must already be aware by now there are no classes in JavaScript. Instead functions in JavaScript may be made to behave like constructors by preceding a function call with the new keyword. This is known as the constructor pattern.

In JavaScript everything is an object except for the primitive data types (boolean, number and string), and undefined. On the other hand null is actually an object reference even though you may at first believe otherwise. This is the reason typeof null returns "object".

Functions in JavaScript are similar to functables in Lua (i.e. they are callable objects). Hence a function can be used in place of an object. Similarly arrays are also objects in JavaScript. On the other hand objects can be thought of as associative arrays.

The most important point however is that there are no classes in JavaScript because JavaScript is a prototypal object oriented language. This means that objects in JavaScript directly inherit from other objects. Hence we don't need classes. All we need is a way to create and extend objects.

Read the following thread to learn more about prototypal inheritance in JavaScript: Benefits of prototypal inheritance over classical?

like image 132
Aadit M Shah Avatar answered Sep 24 '22 02:09

Aadit M Shah