运行 ❯
获取您的
自己
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Object Constructors</h1> <h2>Adding a Property to a Constructor</h2> <p id="demo"></p> <script> // Constructor function for Person Objects function Person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; } // Create 2 Person Objects const myFather = new Person("John", "Doe", 50, "blue"); const myMother = new Person("Sally", "Rally", 48, "green"); // Will Not Work Person.nationality = "English"; // Display Nationality document.getElementById("demo").innerHTML = "The nationality of my father is " + myFather.nationality; </script> </body> </html>