Từ khóa new trong JavaScript là toán tử new, tạo instance của kiểu đối tượng do người dùng định nghĩa.
Ví dụ
<!DOCTYPE html>
<html>
<body>
<p id="test"></p>
<script>
var dept = new Object();
dept.employee = "Amit";
dept.department = "Technical";
dept.technology = "Java";
document.getElementById("test").innerHTML =
dept.employee + " is working on " + dept.technology + " technology.";
</script>
</body>
</html>
Kết quả
Amit is working on Java technology.
Cách hiện đại (ES6+): Dùng Object Literal {} hoặc Class thay vì new Object():
const dept = { employee: "Amit", department: "Technical", technology: "Java" };