Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What techniques can be used to define a class in JavaScript, and what are their trade-offs?

I prefer to use OOP in large scale projects like the one I'm working on right now. I need to create several classes in JavaScript but, if I'm not mistaken, there are at least a couple of ways to go about doing that. What would be the syntax and why would it be done in that way?

I would like to avoid using third-party libraries - at least at first.
Looking for other answers, I found the article Object-Oriented Programming with JavaScript, Part I: Inheritance - Doc JavaScript that discusses object-oriented programming in JavaScript. Is there a better way to do inheritance?

like image 314
Karim Avatar asked Dec 22 '08 23:12

Karim


People also ask

How can we define classes in JavaScript?

Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes but also have some syntax and semantics that are not shared with ES5 class-like semantics.

What is the type of for a class in JavaScript?

In JavaScript, the typeof operator returns the data type of its operand in the form of a string. The operand can be any object, function, or variable.

How are JavaScript classes different?

The most important difference between class- and prototype-based inheritance is that a class defines a type which can be instantiated at runtime, whereas a prototype is itself an object instance.

What are classes in ES6 in JavaScript?

There are two types of Class in ES6: parent class/super class: The class extended to create new class are know as a parent class or super class. child/sub classes: The class are newly created are known as child or sub class. Sub class inherit all the properties from parent class except constructor.


1 Answers

Here's the way to do it without using any external libraries:

// Define a class like this function Person(name, gender){     // Add object properties like this    this.name = name;    this.gender = gender; }  // Add methods like this.  All Person objects will be able to invoke this Person.prototype.speak = function(){     alert("Howdy, my name is" + this.name); };  // Instantiate new objects with 'new' var person = new Person("Bob", "M");  // Invoke methods like this person.speak(); // alerts "Howdy, my name is Bob" 

Now the real answer is a whole lot more complex than that. For instance, there is no such thing as classes in JavaScript. JavaScript uses a prototype-based inheritance scheme.

In addition, there are numerous popular JavaScript libraries that have their own style of approximating class-like functionality in JavaScript. You'll want to check out at least Prototype and jQuery.

Deciding which of these is the "best" is a great way to start a holy war on Stack Overflow. If you're embarking on a larger JavaScript-heavy project, it's definitely worth learning a popular library and doing it their way. I'm a Prototype guy, but Stack Overflow seems to lean towards jQuery.

As far as there being only "one way to do it", without any dependencies on external libraries, the way I wrote is pretty much it.

like image 178
Kenan Banks Avatar answered Sep 28 '22 17:09

Kenan Banks