复制代码 代码如下: <P>// 方法一<BR>var p0 = new Object();<BR>p0.name = "xy";<BR>p0.age = 20;<BR>p0.sayName = function() { alert(this.name); }<BR>p0.showAgeAfterSomeYears = function(year) { return this.age + year };<BR>p0.sayName();<BR>alert(p0.showAgeAfterSomeYears(10)); <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>// 方法三<BR>function Person(name,age){<BR>this.name = name;<BR>this.age = age;<BR>this.sayName = function(){ alert(this.name); }<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>// 方法四<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>// 方法五<BR>var p4 = {};<BR>p4.name = "xy";<BR>p4.age = 20;<BR>p4.sayName = function() { alert(this.name); }<BR>p4.showAgeAfterSomeYears = function(year) { return this.age + year };<BR>p4.sayName();<BR>alert(p4.showAgeAfterSomeYears(10)); <P>