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. vim之vimrc配置文件

    """"""""""""""""&quo ...

  2. 【Js 文件】 相关

    防止浏览器缓存 <script src="/js/common.js?t=<%=DateTime.Now.ToFileTime().ToString()%>>&quo ...

  3. UVA - 10603 Fill(BFS求最小值问题)

    题目: 给出三个杯子(没有刻度线)的容量,起初之后第三个杯子是满的,其他的两个杯子是空的,容量分别是a.b.c.问最少需要倒多少升水才能让某一个杯子中的水有d升?如果不能恰好做到d升,就让某一个杯子里 ...

  4. World Cup(The 2016 ACM-ICPC Asia China-Final Contest dfs搜索)

    题目: Here is World Cup again, the top 32 teams come together to fight for the World Champion. The tea ...

  5. xfce 安装文泉驿字体

    下载文泉驿字体 #拷贝字体到目录/usr/share/fonts/wqy#创建字体缓存 mkfontscale # 在当前目录下生成fonts.scale文件 mkfontdir # 在当前目录下生成 ...

  6. 在mac上面运行cherrytree

    下载源码包 wget http://www.giuspen.com/software/cherrytree-0.38.4.tar.xz 解压 tar -xvf cherrytree-0.38.4.ta ...

  7. Python基础-获取当前目录,上级目录,上上级目录

    import os print '***获取当前目录***' print os.getcwd() print os.path.abspath(os.path.dirname(__file__)) pr ...

  8. Unity对象的所有组件深拷贝与粘贴

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/51454847 作者:car ...

  9. JSON.parseObject将json字符串转换为bean类,是否大小写敏感区分---https://blog.csdn.net/mathlpz126/article/details/80684034

    JSON.parseObject将json字符串转换为bean类,是否大小写敏感区分 https://blog.csdn.net/mathlpz126/article/details/80684034

  10. [luoguP2158] [SDOI2008]仪仗队(数论)

    传送门 可以看出 (i, j) 能被看到,(i * k, j * k) 都会被挡住 暴力 所以 gcd(i, j) == 1 的话 ans ++ 那么可以枚举一半(中轴对称),求解答案,只能拿30分 ...