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

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

  • 相关阅读:
    find文件后cp、rm
    值得加分的
    对老师的评价
    关于Statement 与 PreparedStatement
    每日日报2021.1.14
    每日日报2020.1.12
    每日日报2021.1.11
    给自己算的加分项
    期末了,给老师提建议。
    每日日报2020 12/19
  • 原文地址:https://www.cnblogs.com/JimmyLuo/p/6667965.html
Copyright © 2011-2022 走看看