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_线程类的基本功能

    Thread类是实现了Runnable接口 其方法有: start()开始:开始线程 run()跑:线程内容 currentThread()现在的线程:返回当前线程 getName():获取线程名 s ...

  2. uploadify的简单使用

    简单的图片上传: 1.进入官网下载uploadify插件:http://www.uploadify.com/download/ 2.导入uploadify插件提供的css样式和类库: <link ...

  3. 【Redis】二、Redis高级特性

    (三) Redis高级特性   前面我们介绍了Redis的五种基本的数据类型,灵活运用这五种数据类型是使用Redis的基础,除此之外,Redis还有一些特性,掌握这些特性能对Redis有进一步的了解, ...

  4. XGBoost参数中文翻译以及参数调优

    XGBoost:参数解释:https://blog.csdn.net/zc02051126/article/details/46711047 机器学习系列(11)_Python中Gradient Bo ...

  5. jdbcTemplate传参使用Map或List

    List传参方式 举个例子 sql = "select * from table where id=? and param=?": sql中的参数要用?形式,然后使用list.ad ...

  6. [gulp]Cannot find module 'orchestrator'

    从github 将项目 clone到本地后,运行gulp 启动项目时,出现这个问题的原因是: 1.clone 项目连同 nodemodules目录也一起下载到本地. 解决方式: 1.从本地删除node ...

  7. 洛谷——P3173 [HAOI2009]巧克力

    P3173 [HAOI2009]巧克力 题目描述 有一块n*m的矩形巧克力,准备将它切成n*m块.巧克力上共有n-1条横线和m-1条竖线,你每次可以沿着其中的一条横线或竖线将巧克力切开,无论切割的长短 ...

  8. JDBC—简单的数据交互

    东西传值.交互 ps:里面的时间为手动添加.如果需要更改为系统日期,那就把里面 加了  //  的语句去掉  ,然后再加  包驱动. 新建一个1.jsp 用来当做用户输入东西的界面 再新建 第二个页面 ...

  9. js之条件判断

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Android layer-list(3)

     Android layer-list(3) 在附录文章3.4的基础上,就Android layer-list再写一个较为复杂的应用. 先写布局文件,该布局涉及到LinearLayoutCompa ...