运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,暗/亮
前往 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"); // Add a new Property Person.prototype.nationality = "English"; // Display new Property document.getElementById("demo").innerHTML = "The nationality of my father is " + myFather.nationality; </script> </body> </html>