Extending Object.prototype JavaScript

前端 未结 5 514
终归单人心
终归单人心 2020-11-27 05:30

I am not asking if this is okay:

Object.prototype.method = function(){};

This is deemed evil by pretty much everyone, cons

5条回答
  •  醉话见心
    2020-11-27 06:14

    Well in "JavaScript: the good parts", there is a similar function, i think that is very usefull to improve javascript base objects (like String, Date, etc..), but just for that.

    // Add a method conditionally. from "JavaScript: the good parts"
    
    Function.prototype.method = function (name, func) {
        if (!this.prototype[name]) {
            this.prototype[name] = func;
        }
    }
    

提交回复
热议问题