js编程-面相对象

时间:2023-03-08 20:15:16
//js面相对象编程
//定义constructor构造方法
function myFn(name,sex){
this.name = name;
this.sex = sex;
}
//用prototype追加属性方法
myFn.prototype.getName = function(inter){
console.log(this.name);
console.log("兴趣:" + inter);
return this.name;
} //实例化myFn
var newMy = new myFn();
newMy.name = "黄晓明";
newMy.sex = "男";
newMy.getName("唱歌");