zoukankan      html  css  js  c++  java
  • JavaScript Patterns 2.5 (Not) Augmenting Build-in Prototypes

    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...
         };
    }
  • 相关阅读:
    关于binary log一点总结[转]
    使用mysql索引技巧及注意事项
    优化php性能的一点总结
    html静态页面实现微信分享思路
    MySql字符串函数使用技巧
    Oracle计算时间差函数
    oracle10g获取Date类型字段无时分秒解决办法!
    Oracle常用函数
    COALESCE操作符
    关于null的操作
  • 原文地址:https://www.cnblogs.com/haokaibo/p/Not-Augmenting-Build-in-Prototypes.html
Copyright © 2011-2022 走看看