1

kindly from couple of days till now I am confusing about how are some articles say that JavaScript Supports Classes and many FAQS, StackOverflow Threads and articles say the opposite? please clearly answer ,

Does JS support classes or not?

and the important question is that why I feel that there's no stability in this language?

for example, you will not find any js classes lessons except in Mozzila Website and a few others only. in PHP, I am always noticing that the difference or confusion maybe in function usage, function support, but I didn't meet before a confusion about if a programming language or client side language is supporting OOP Classes or not. I hope I could write my confusion clearly.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
mina
  • 141
  • 1
  • 10
  • 1
    Javascript has seen an explosive growth, and when talking about "javascript" today you are in reality talking about quite a few different languages (ES5, ES6, typescript, dart, etc). When using different variations of the EcmaScript standard you might need to use a set of tools to compile (convert) the code. I suggest you read [this blog post on js confusion](https://hackernoon.com/how-it-feels-to-learn-javascript-in-2016-d3a717dd577f), and then try to search up "js 2016" or similar. It's a mess, but try to limit it by being selective to what you delve into. – JimL Jan 15 '17 at 10:09
  • No, there's no support for traditional classes in JS. As the [docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes) says, the new ES6 `class` feature is "_syntactical sugar over JavaScript's existing prototype-based inheritance_". – Teemu Jan 15 '17 at 10:13
  • @JimL : i will read it and return back to discuss ,temporarily thank you , but should really i have to specify the JS version before learning ? – mina Jan 15 '17 at 10:21

3 Answers3

3

JavaScript classes introduced in ECMAScript 2015 are addition to JavaScript's existing prototype-based inheritance. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance, however the class syntax is not introducing a new object-oriented inheritance model to JavaScript.

One way to define a class is using a class declaration. To declare a class, you use the class keyword with the name of the class ("Car" for example):

class Car{
  constructor(weight, year) {
    this.weight= weight;
    this.year= year;
  }
}

You first need to declare your class and then access it (unlike declaration of function), otherwise you will get a ReferenceError.

Bud Damyanov
  • 30,171
  • 6
  • 44
  • 52
1

In JavaScript, as of ECMA-Script 2015 and above, there's a new and fancy class syntax, but it's all about a syntactic sugar layer on top of prototype chain.

See this code sample:

// ECMA-Script 2015 and above
class A {

}

class B extends A {

}

// ECMA-Script 5 or what's really happening behind the scenes:
function A() {}

function B() {}
B.prototype = Object.create(A.prototype);

Are classes in JavaScript? From a high level point of view, yes, they're. But it's not a type system like you would find in other programming languages like C++, C#, Java, Ruby, Python...

Further reading

Community
  • 1
  • 1
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
1

Yes of course, javascript support classes. As javascripts is functional programming language and almost all things are based on functions, So if you want to use class in javascript you have to use Constructor functions. Constructor functions work like this

function Vehicle(make, model, color) {
        this.make = make,
        this.model = model,
        this.color = color,
        this.getName = function () {
            return this.make + " " + this.model;
        }`enter code here`
}

and the other confusion of your is related to stability about javascript. So according to my point of view javascript is more stable than php and some other languages. You can check this article to clear you confusion in more detail.

https://blog.carbonteq.com/javascripts-importance-and-comparison-with-php/

  • Note: As an employee of Carbon Teq, you **must** disclose your affiliation with the site you've linked to. See: https://stackoverflow.com/help/promotion – Nisse Engström Jan 19 '19 at 18:52