What is the new ke...
What is the new ke...
The new
keyword in JavaScript is used to create a new instance of an object that has a constructor function. When you use new
with a function, several steps are taken to create a new object:
[[Prototype]]
property (often accessible as __proto__
) is set to the prototype of the constructor function, establishing the prototype chain.this
variable within the constructor function is bound to the new object, allowing properties and methods to be assigned to it using this
.new
operator will return the new object that was created. If a non-primitive value is returned by the constructor, that value will be returned instead of the new object.Using the new
keyword ensures that each instance created is unique and has its own set of properties and methods, as defined in the constructor function. It also correctly sets up the object's prototype chain, which is essential for object inheritance in JavaScript.
The new
keyword also changes the behavior of the this
keyword inside the constructor function. Without new
, this
would refer to the global object (in non-strict mode) or...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào