-1

In one place on the Mozilla Developer Network, I read that JavaScript does not have support for classes. In another, I read that JavaScript has a class statement. Which is it? What is going on here? Is it somehow different from the classes in other programming languages? If it is different, then in what way?

Melab
  • 2,594
  • 7
  • 30
  • 51
  • Where are the MDN links? –  Sep 18 '16 at 16:17
  • *"Is it somehow different from the classes in other programming languages?"* You should always approach a language with the assumption that its constructs are different from those in other languages. –  Sep 18 '16 at 16:18
  • 1
    *"If it is different, then in what way?"* Different from which language(s)? The answer is going to entirely depend on those languages to which you're comparing it. There's no universal, objective definition of what a "class" *must* be in a language. –  Sep 18 '16 at 16:19

4 Answers4

0

JavaScript has classes introduced in ES6.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

Previous to this JavaScript has what to is referred to as Constructor Functions.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
0

In ES 6 they have introduced classes. You can read some features added in ES 6 here at : http://es6-features.org/#ClassDefinition

0

Yes, javascript does have classes. You can define them as objects

var Person = function (firstName) {
  this.firstName = firstName;
  console.log('Person instantiated');
};
var person1 = new Person('Alice');
var person2 = new Person('Bob');

// Show the firstName properties of the objects
console.log('person1 is ' + person1.firstName); // logs "person1 is Alice"
console.log('person2 is ' + person2.firstName); // logs "person2 is Bob"

or using the new es6 syntax:

class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}
var polygon = new Polygon(1,1);

Note that browser support for the es6 syntax is limited.

Steven Xu
  • 25
  • 5
0

As founder of an OOP js library, I will recommend to use OODK-JS which works on all supported js environments and allows to design web application using classes, interfaces and namespaces (plus side libraries to work with objects as big brothers, Java, PHP, C++ does: cloning, serialization, multi-threading, webservices and much more)

OBDM
  • 177
  • 3