How best to inherit from native JavaScript object? (Especially String)

后端 未结 4 1990
北恋
北恋 2021-02-12 18:01

I\'m a long-time browser but a first time participator. If I\'m missing any etiquette details, please just let me know!

Also, I\'ve searched high and low, including this

4条回答
  •  独厮守ぢ
    2021-02-12 18:33

    I would look into creating a new object, using the native object as a backing field and manually recreating the functions for the native objects. For some very rough, untested sample code...

    var myString = {
        _value = ''
        ,substring:_value.substring
        ,indexOf:_value.indexOf
    }
    

    Now, I'm sure this wont work as intended. But I think with some tweaking, it could resemble on object inherited from String.

提交回复
热议问题