使用构造器创建 JavaScript 对象
- <!DOCTYPE html>
- <html>
- <body>
- <h1>JavaScript 对象构造器</h1>
- <p id="demo"></p>
- <script>
- // Person 对象的构造器函数
- function Person(first, last, age, eye) {
- this.firstName = first;
- this.lastName = last;
- this.age = age;
- this.eyeColor = eye;
- }
- // 创建 Person 对象
- var myFriend = new Person("Bill", "Gates", 62, "blue");
- // 显示年龄
- document.getElementById("demo").innerHTML =
- "My friend is " + myFriend.age + ".";
- </script>
- </body>
- </html>