javascript - 在 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}`;
}
什么是尊重?
解决方案
推荐阅读
- reactjs - 一起使用时,TooltipClasses 和 FabProps 不适用于 Material UI React 中的 SpeedDialAction
- omnet++ - 如何在 OMNeT++ 的仿真过程中将 simTime() 设置为零
- spring - API 调用失败:HHH000206: hibernate.properties not found 更改 Spring Boot 的数据源时出现异常
- angularjs - django-rest-social-auth 中的“代码”参数是什么?
- python-3.x - 我可以在 xlsxwriter 中使用列标题,而不是在格式化时使用列字母
- azure - zip部署的Web应用程序部署失败?
- ruby - 拆分字符串错过了用于拆分它的单词
- javascript - $(window).scrollTop(value) 在移动设备上不起作用
- typescript - 创建一个从方法中提取返回值的泛型类型
- java - 如何使用列表视图调用对话框片段并从片段中搜索过滤器并将父片段中的选定项目值保存为字符串?