String prototype modifying itself

后端 未结 4 2131
广开言路
广开言路 2021-02-20 16:09

As far as i know it\'s not possible to modify an object from itself this way:

String.prototype.append = function(val){
    this = this + val;
}

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-20 17:13

    Strings are immutable; what you're asking is like saying, "Why can't I do:

    Number.prototype.accumulate = function (x) {
        this = this + x;
    };
    

    ...?"

提交回复
热议问题