首页 > 解决方案 > 在 JavaScript 中使用原型添加函数

问题描述

我对使用原型进行继承的好处感到困惑。在构造函数内部定义函数(方法)和将函数添加到原型属性有什么区别,例如:

function Car(name,model){
            this.name = name;
            this.model = model;
            this.GetSummary = function(){
                `The car is ${this.name} and the model is ${this.model}`;
            }
        }

现在,如果我从构造函数 Car 中删除 this.GetSummary 并将其添加到原型中,如下所示:

Car.prototype.GetSummary = function(){
                    `The car is ${this.name} and the model is ${this.model}`;
                }

什么是尊重?

标签: javascript

解决方案


推荐阅读