The
new
keyword crreate an instance of a user-defined object type or of one of the built-in object types that has a constructor function.
- Syntax:
new constructor(arguments);
The
this
keyword refers to an object. Which object depends on howthis
is being used or called. Thethis
keyword refers to different objects depending on how it is used.
- In an object method,
this
refers to the object. - In an alone,
this
refers to the global object. - In a function,
this
refers to the global object. - In a function, in strict mode,
this
is undefined. - In an event,
this
refers to the element that received the event. - Method like
call()
,apply()
, andbind()
->this
refers to any object.
Classes are template for creating objects. They encapsulate data with code to work on that data. Classes in JavaScript are built on prototypes but also have some syntax and semantics that are not shared with ES5 class-like semantics.
- Syntax:
class ClassName {
constructor() { ... }
}