js - 构造函数默认参数

痞子三分冷 提交于 2020-01-28 09:01:34
var pen = function(name,color,price){
    this.name = name;
    this.color = color;
    if(price == undefined){
        this.price = 9.9;
    }
    else{
        this.price = price;
    }
}
var Rpen = new pen("Mao","Blue",200);
var Bpen = new pen("Zhao","Red")
console.log(Rpen.name+"\n"+Rpen.color+"\n"+Rpen.price+"\n");
console.log(Bpen.name+"\n"+Bpen.color+"\n"+Bpen.price);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!