<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript 对象</h2> <p id="demo"></p> <script> function Person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; } var myFather = new Person("John", "Doe", 50, "blue"); var myMother = new Person("Sally", "Rally", 48, "green"); document.getElementById ( " Demo " ) .innerHTML = " My paternal age is " + myFather.age + " My mother's age. " + myMother.age; </ Script > </ body > </ HTML >
<! DOCTYPE HTML > < HTML > < head > < Meta charset = "UTF-. 8" > < title > novice tutorial (runoob.com) </ title > </ head > < body > < H2 > the JavaScript Object </ H2 > < the p- > you can not add new attributes to the constructor. </ P > < P ID = "Demo" > </ P > < Person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; } Person.nationality = "English"; var myFather = new Person("John", "Doe", 50, "blue"); var myMother = new Person(" Sally " , " Rally " , 48 , " Green " ); document.getElementById ( " Demo " ) .innerHTML = " My father's nationality is " + myFather.nationality; </ Script > </ body > </ HTML >
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript 对象</h2> <p id="demo"></p> <script> function Person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; this.nationality = "English"; } var myFather = new Person("John", "Doe", 50, "blue"); var myMother = new Person("Sally", "Rally", 48 , " Green " ); document.getElementById ( " Demo " ) .innerHTML = " My father's nationality is " + myFather.nationality + " . My mother's nationality is " + myMother.nationality; </ Script > </ body > </ HTML >
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript 对象</h2> <p id="demo"></p> <script> function Person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; } Person.prototype.nationality = "English"; var myFather = new Person("John", "Doe", 50, "blue"); document.getElementById("demo").innerHTML = "我父亲对国籍是 " + myFather.nationality; </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>JavaScript 对象</h2> <p id="demo"></p> <script> function Person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; } Person.prototype.name = function() { return this.firstName + " " + this.lastName }; var myFather = new Person("John", "Doe", 50, "blue"); document.getElementById("demo").innerHTML = "我对父亲是 " + myFather.name(); </script> </body> </html>