Difference between creating object stability through constructor function VS class and constructor

1

I do not understand the difference between these two examples, one with class and another with a constructor function

function personOne(name, age) { 
  this._name = name;   
  this._age = age;
}

var p = new personOne("David", 21);
var p2 = new personOne("Fran", 28);
console.log(p._name + ' ' + p._age);
console.log(p2._name + ' ' + p2._age);



class personTwo{ 
  constructor(name, age){
    this._name = name;
    this._age = age;
  }

}
var p = new personTwo('David', 21);
var p2 = new personTwo('Fran', 28);
console.log(p._name + ' ' + p._age);
console.log(p2._name + ' ' + p2._age);
    
asked by francisco dwq 12.01.2018 в 16:52
source

1 answer

3

Both do exactly the same. There are no differences between the two when compiling. class is just a new syntax for clearing an object. As it says MDN :

  

JavaScript classes, introduced in ECMAScript 2015, are   mainly a syntax about the existing inheritance based on   JavaScript prototypes. Class syntax does not introduce a new   object-oriented inheritance model to JavaScript.

This was introduced to make javascript less complex, more modern and easier to learn for people who move to the language. For example, the developers of languages such as c # and Java find it much easier to transpose the language since it is a syntax that they know.

    
answered by 12.01.2018 в 16:58