javascript - JavaScript 有条件地添加方法
问题描述
我正在阅读“JavaScript: The Good Parts”并且遇到了这个令人困惑的代码片段
Function.prototype.method = function( name, func){
if (!this.prototype[name]){
this.prototype[name] = func;
}
};
书中说“一种防御技术是仅在已知方法缺失时才添加方法。” 我对此的问题是:
- 这段代码是如何工作的?
- 我将如何使用我们创建的这种方法
- 为什么是“this.prototype”而不是“this.method”或“this.name”
感谢您的时间
解决方案
推荐阅读
- visual-studio - 访问 EnvDTE80.ProjectKinds.vsProjectKindSolutionFolder 会产生互操作错误
- python - 创建日期时间列
- vsix - 如何在 VS2019 预览版 VSIX 中添加类别过滤器标题?
- javascript - 使用节点在mongo中保存文档
- reactjs - 反应 TypeScript 模态
- twitter-bootstrap-3 - 如何使引导下拉父链接可点击
- spring-boot - 我是否需要重新启动 Tomcat 才能使其配置更改生效?
- android - 更新android应用程序时删除obb文件
- c# - IIS:处理程序“aspNetCore”的模块列表中有一个错误模块“AspNetCoreModuleV2”
- php - 致命错误:不能使用“对象”作为类名,因为它是保留的