Disadvantage

  1. Other developers using your code will probably expect the built-in JavaScript methods to work consistently and will not expect your additions.
  2. Properties you add to the prototype may show up in loops that don't use hasOwnProperty(), so they can create confusion.

Augment build-in prototypes under all of the conditions below:

  1. It's expected that future ECMAScript versions or JavaScript implementations will implement this functionality as a built-in method consistently. For example, you can add methods described in ECMAScript 5 while waiting for the browsers to catch up. In this case you're just defining the useful methods ahead of time.
  2. You check if your custom property or method doesn't exist already—maybe already implemented somewhere else in the code or already part of the JavaScript engine of one of the browsers you support.
  3. You clearly document and communicate the change with the team.  

If these three conditions are met, you can proceed with the custom addition to the prototype, following this pattern:

if (typeof Object.protoype.myMethod !== "function") {
Object.protoype.myMethod = function () {
// implementation...
};
}

JavaScript Patterns 2.5 (Not) Augmenting Build-in Prototypes的更多相关文章

  1. JavaScript Patterns 7.1 Singleton

    7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...

  2. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  3. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  4. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  5. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  6. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  7. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  8. JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns

    In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...

  9. JavaScript Patterns 5.9 method() Method

    Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...

随机推荐

  1. Handling unhandled exceptions and signals

    there are two ways to catch otherwise uncaught conditions that will lead to a crash: Use the functio ...

  2. RocketMQ学习笔记(13)----RocketMQ的Consumer消息重试

    1. 概念 Producer端重试: 生产者端的消息失败,也就是Producer往MQ上发消息没有发送成功,比如网络抖动导致生产者发送消息到MQ失败. 这种消息失败重试我们可以手动设置发送失败重试的次 ...

  3. vuex与redux,我们都一样

    vuex与redux的主要区别: redux:生成的全局数据流是通过每个组件的props逐层传递到各个子组件的,通过@connect装饰器绑定在this.props上面. vuex :生成的全局数据则 ...

  4. 用php生成HTML文件的类

    目的 用PHP生成HTML文档, 支持标签嵌套缩进, 支持标签自定义属性 起因 这个东西确实也是心血来潮写的, 本来打算是输出HTML片段用的, 但后来就干脆写成了一个可以输出完整HTML的功能; 我 ...

  5. jupyter notebook的插件安装及文本格式修改

    jupyter notebook的插件安装及文本格式修改 1.jupyter notebook拓展插件安装 启动jupyter notebook : 打开控制台输入命令 jupyter noteboo ...

  6. phpcms 短信替换

    后台表单向导文件路径: [/www/wwwroot/phpcms/phpcms/modules/formguide/templates/formguide_info_list.tpl.php] pub ...

  7. shrink&split

    shrink将分片数按因子缩减.hard link segment文件.因缩减前后hash一致,不需要rehash.如:0 ,1 , 2, 3, 4, 5, 6, 7, 8.9个分片缩减成3个:0 [ ...

  8. 【Codeforces 356A】Knight Tournament

    [链接] 我是链接,点我呀:) [题意] n个人矩形m场比赛 每场比赛由编号为li~ri且之前没有被淘汰的人进行. 已知第i场的winner是xi winner会把这一场其他所有的人都淘汰. 问你n个 ...

  9. Leetcode 131.分割回文串

    分割回文串 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...

  10. 用Windows自带DOS命令提示符 制作U盘启动盘

    Windows & DOS命令提示符 & U盘 & 启动盘 用Windows自带DOS命令提示符 制作U盘启动盘.docx http://xgqfrms.blog.163.co ...