- //用构造函数创建对象,利用原型为函数添加方法
- function Person(name,age,sex){
- this.Name=name;
- this.Age=age;
- this.Sex=sex;
- }
- Person.prototype.play=function(){
- return "我是:"+this.Name+",姓名:"+this.Age+",年龄:"+this.Sex+"";
- }
- Person.prototype.fav=function(food){
- return "我爱吃"+food+"";
- }
- var per=new Person(‘xiao‘,12,‘nv‘);
- alert(per.play());
- alert(per.fav("苹果"));
时间: 2024-10-05 23:58:20