我们为什么要使用原型?

时间:2019-12-30 11:51:21

标签: javascript prototype prototype-programming

function Pet(name, species){
    this.name = name;
    this.species = species;
}
Pet.prototype.view = function view(){
    return this.name + " is a " + this.species + "!";
};

我想我可以通过这种方式实现上述功能

function Pet(name, species){
    this.name = name;
    this.species = species;
    this.view = function () {
        return this.name + " is a " + this.species + "!";
    }
}

那么原型的用途是什么?

0 个答案:

没有答案