我想要為String添加個自定義方法,并使用該方法對字符串進行處理,網上有找到以下方法: Object.assign(String.prototype,{ 方法1() { ... } })但是在使用時沒有效果,如下Object.assign(String.prototype,{ rsaEnscrypt (publicKey) { let rsaProvider = new JSEncrypt(); rsaProvider.setPublicKey(publicKey); let strEncrypt = rsaProvider.encrypt(this.replace(/\+/g, '%2B')); return strEncrypt; }})this.login.Password.rsaEnscrypt();報錯:Property 'rsaEnscrypt' does not exist on type 'string'.
es6中該如何為String內置對象添加自定義方法?
尚方寶劍之說
2018-12-20 18:19:31