复制代代码如下: <p>// 方法一<br>var p0 = new Object();<br>p0.name = "xy";<br>p0.age = 20;<br>p0.sayName = function() {alert (この名前); }<br>p0.showAgeAfterSomeyears = function(year) { return this.age year };<br>p0.sayName();<br>alert(p0.showAgeAfterSomeyears(10));</p> <p>// 方法二<br>function Person(){<br>}<br>var p1 = new Person();<br>p1.name = "xy";<br>p1.age = 20; <br>p1.sayName = function() {alert(this.name); }<br>p1.showAgeAfterSomeyears = function(year) { return this.age year };<br>p1.sayName();<br>alert(p1.showAgeAfterSomeyears(10));</p> <p>// 方法三<br>function person(name,age){<br>this.name = name;<br>this.age = age;<br>this.sayName = function(){alert(this 。名前); }<br>this.showAgeAfterSomeyears = function(year) { return this.age year };<br>}<br>var p2 = new Person("xy",20);<br>p2.sayName();<br>alert(p2.showAgeAfterSomeyears(10));</p> <p>// 方法四<br>var p3 = {<br>name : "xy",<br>age : 20,<br>sayName : function(){alert(this.name); },<br>showAgeAfterSomeyears : function(year) { return this.age year }<br>};<br>p3.sayName();<br>alert(p3.showAgeAfterSomeyears(10));</p> <p>// 方法五<br>var p4 = {};<br>p4.name = "xy";<br>p4.age = 20;<br>p4.sayName = function() {alert(this 。名前); }<br>p4.showAgeAfterSomeyears = function(year) { return this.age year };<br>p4.sayName();<br>alert(p4.showAgeAfterSomeyears(10));</p> <p>