Câu hỏi phỏng vấn Javascript
Câu hỏi

What are the differences between ES6 class and ES5 function constructors?

Câu trả lời

When discussing the differences between ES6 class and ES5 function constructors, it's important to understand that both are mechanisms in JavaScript for creating new objects and managing inheritance, but they have distinct syntax and behavior.

ES6 Class Constructors

  • Syntax: ES6 introduces a class keyword, providing a syntax that is similar to class-based object-oriented languages. This makes it easier for developers from other OOP languages to understand and use.

    javascript Copy
    class User {
      constructor(name, age, gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
      }
      print() {
        console.log(`${this.name} has an age of ${this.age} and gender of ${this.gender}`);
      }
    }
  • Inheritance: ES6 simplifies inheritance through the extends keyword, making it more intuitive to create a class hierarchy.

  • Super Keyword: The super keyword is used in ES6 to call the constructor of a parent class, ensuring proper initialization of the inherited properties.

  • Static Methods: ES6 formalizes the declaration of static methods using the static keyword.

  • New Features: ES6 classes support new features like property declarations, private fields, and methods, including static ones[3].

ES5 Function Constructors

  • Syntax: ES5 uses function constructors with a less intuitive syntax that is unique to JavaScript and not generally found in other object-oriented programming languages.

    javascript Copy
    function User(name, age, gender) {
      this.name = name;
      this.age = age;
      this.gender = gender;
      thi...
middle

middle

Gợi ý câu hỏi phỏng vấn

entry

What is typeof operator in javascript?

middle

When should we use generators in ES6?

middle

What is generator in JS?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào