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

Explain difference between: function Person(){} , va...

Câu trả lời

To answer the interview question regarding the differences between function Person(){}, var person = Person(), and var person = new Person(), it's important to understand the context in which each is used in JavaScript, particularly focusing on function declarations, function calls, and object instantiation.

  1. function Person(){}:

    • This is a function declaration that defines a function named Person. It doesn't execute the function but merely sets up the function structure in memory. The function can be called later in the code. This function, when called, will execute the code block defined within its braces {}. If used with the new keyword, it can act as a constructor to create new objects.
  2. var person = Person():

    • This line of code calls the function Person and assigns its return value to the variable person. If the Person function does not explicitly return a value, person will be assigned undefined. This usage does not involve object creation directly unless the Person function itself returns an object. This is a simple function call and is not used for creating an instance of an object in the context of object-oriented programming.
  3. var person = new Person():

    • This statement is used for creating a new instance of an object using the Person function as...
senior

senior

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

expert

Compare Async/Await and Generators usage to achive same functionality

middle

What is the difference between document load event and document DOMContentLoaded event?

junior

Explain Null and Undefined in JavaScript

Bình luận

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

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