📜  ES6 类和 ES5函数构造函数的区别

📅  最后修改于: 2022-05-13 01:56:22.019000             🧑  作者: Mango

ES6 类和 ES5函数构造函数的区别

在本文中,我们将讨论 ES6 类和 ES5函数构造函数之间的区别。两者都用于创建新对象,但它们之间仍然存在一些差异。

ES6 类构造函数: ES6 类构造函数的工作方式与其他面向对象语言中的类构造函数完全相同。它们用于创建新对象。

Javascript


Javascript


输出:

"Roy has an age of 19 and gender of Male"

ES5函数构造函数: ES5函数构造函数也用于创建对象。上面的例子可以通过使用函数构造函数进行如下修改。

Javascript


输出:

"Roy has an age of 19 and gender of Male"

ES6 类和 ES5函数构造函数的区别:

                          ES6 class constructors                         ES5 function constructors
As discussed above ES6 class constructors creates objects by adding function to their prototypes (Blueprint).ES5 function constructors also create objects along with inheritance property. 
It ensures that this keyword used by the developer is referring to the object being created by the developer. Any function can be used as a function constructor and it primarily focuses on the creation of reusable object creation code.
Its syntax is similar to object creation in other object-oriented programming languages.Its syntax is unique and is not generally found in other object-oriented programming languages.
This can be said to be a syntax base for constructor functions and instantiate objects using a new operator.This also uses a new operator for object creation but focuses on how the objects are being instantiated.