oop - How do I extend (with a new name) a static function in javascript without affecting the original? -


i'm having trouble working out how extend static function (momentjs) can override methods, without altering original function.

to clear know how extend instance of moment override functions, want extend library directly own named instance of moment can use in same way momentjs.

as example, i'd able following

extendedmoment().customfunction() //do custom extendedmoment().tostring() //use customised tostring() method extendedmoment().format() //use original momentjs method 

i've tried few options copying prototype etc, editing prototype of new extendedmoment function seems affect original.

update: answered below @patrickroberts

after digging the source, can't directly extend library because there several scoped functions not exposed, , moment() wrapper of wrapper of wrapper of wrapper of constructor. here's best can reusing same extended prototype rather assigning scoped functions inside factory extension:

function extendedmoment() {    var self = moment();        self.__proto__ = extendedmoment.prototype;        return self;  }    extendedmoment.prototype.__proto__ = moment.prototype;    extendedmoment.prototype.tostring = function(){    return this.format('yyyy-mm-dd')  }    document.write("original: " + moment().tostring());  document.write("<br/>");  document.write("extended: " + extendedmoment().tostring());
<script src="http://momentjs.com/downloads/moment.min.js"></script>

the way works replaces instance's prototype (which moment.prototype) extendedmoment.prototype inside factory extension, reusing same tostring function instances of extendedmoment.

edit

i caught myself using term "constructor" extendedmoment corrected myself. factory extension, since static function @zakhenry pointed out. apologies misnomer.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -