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. java中随机生成字符串的方法(三种)

    org.apache.commons.lang(2.6): 链接:https://pan.baidu.com/s/1k_oeA5AjSt6evoR7zT8gpQ 提取码:yhl5 1.生成的字符串每个 ...

  2. SIGFPE能处理上下溢吗?

    SIGFPE可以报告算术异常.包括上下溢.除零等. C标准库说: 各种实现报告的异常类型区别也很大,但几乎没有实现会报告整型溢出. 真的吗?我测试了一下,发现在GCC上,整型.浮点上下溢都不会报告,但 ...

  3. 迷宫问题 POJ - 3984 (搜索输出路径)

    题目大意 题目不需要大意,poj居然还有中文题 鸣谢 特别鸣谢ljc大佬提供的方法!!! 解法 我们可能输出个最短路径的长度比较简单,但是输出最短路径真的是没有做过,这里有一种简单的方法 因为我们的d ...

  4. TestNG超时测试

    用@Test(timeOut = XXX) 指定超时时间,单位是毫秒 package com.janson; import org.testng.annotations.Test; public cl ...

  5. python学习,使用requests库来模拟登录github,post请求。

    这次我们要模拟登录的页面是 https://github.com/login 首先我们先尝试着登陆一遍分析一下请求, 打开开发者工具下的network选项, 可以很清楚的看到这个会话session,而 ...

  6. Android 找不到资源的问题

    偶尔会遇到R.layout.***或R.id.***找不到资源的问题,明明在文件夹中有啊,那为什么嘞? 结合我自己遇到的情况和网上的资料,总结出以下几点可能的原因: 导入了android.R.这个是最 ...

  7. 自己写一个HashMap

    package cn.aresoft; /** * HashMap原理 * * @author develp * HashMap是一种以键值对存储数据的数据结构,简单的来说是这样.内部怎么实现的呢?实 ...

  8. BNUOJ 23905 滑雪

    滑雪 Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on UESTC. Original ID: 13096 ...

  9. HDU 1130

    题目大意 给定节点数 , 求通过这么多个节点能得到的二叉树的组成方式 用卡特兰数解决 f[n] = (4*n-2) * f[n-1] / (n+1); 递归不断解决 /** * @(#)Main.ja ...

  10. [luoguP2285] [HNOI2004]打鼹鼠(DP)

    传送门 设f[i]表示i个鼹鼠出现后,打死鼹鼠的最大值 动态转移方程:f[i]=max{f[j]+1}, 条件:abs(x[i]-x[j])+abs(y[i]-y[j])<=time[i]-ti ...