方法只不过是附加在对象上的函数。
在构造器函数内部定义对象的方法:
1 <!DOCTYPE html> 2 <html> 3 <body> 4 <script> 5 function person(firstname,lastname,age,eyecolor) 6 { 7 this.firstname=firstname; 8 this.lastname=lastname; 9 this.age=age; 10 this.eyecolor=eyecolor; 11 12 this.changeName=changeName; 13 function changeName(name) 14 { 15 this.lastname=name; 16 } 17 } 18 myMother=new person("Steve","Jobs",56,"green"); 19 myMother.changeName("Ballmer"); 20 document.write(myMother.lastname); 21 </script> 22 23 </body> 24 </html>
运行结果:Ballmer
原文地址:https://www.cnblogs.com/sanerandm/p/8353430.html
时间: 2024-10-29 13:18:14