如何使用 new 运算符创建 JavaScript 对象?
javascriptweb developmentfront end technology
JavaScript 中的 new 关键字是 new 运算符,它创建用户定义对象类型的实例。
示例
您可以尝试运行以下代码,使用 new 运算符创建 JavaScript 对象 −
<!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>
输出
Amit is working on Java technology.