zoukankan      html  css  js  c++  java
  • 基于webpack模块加载,ts里对系统对象prototype的扩展

    用systemJS的时候,这样写是可以:

    constructor() {
            // 对Date的扩展,将 Date 转化为指定格式的String
            // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
            // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
            // 例子:
            // (new Date()).format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
            // (new Date()).format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
            Date.prototype.format = function (fmt) {
                ……
            }
    }

    但是换成webpack后,这样就报错了,识别不了,查了一些资料,换成下面的就可以了。

    constructor() {
         // 对Date的扩展,将 Date 转化为指定格式的String
            // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
            // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
            // 例子:
            // (new Date()).format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
            // (new Date()).format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
            Date.prototype['format'] = function (fmt) { 
                ……
            }
    }

    或者用这种方法http://stackoverflow.com/questions/12766117/how-does-prototype-extend-on-typescript

    觉得这种方法不是很好,也没亲测。

  • 相关阅读:
    TSQL 基础学习 04
    第13章 网络编程
    Oracle 第一天
    第11章 进程与多线程
    Linux 第06天
    构造Json对象串工具类
    第14章 数据库
    第07章 集合
    YARNMR 大数据第二天
    第12章 多媒体
  • 原文地址:https://www.cnblogs.com/JimmyLuo/p/6667965.html
Copyright © 2011-2022 走看看