改变字符串值的字符串原型 [英] String prototype changing the string value

查看:53
本文介绍了改变字符串值的字符串原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改其原型函数中的字符串值:

I'm trying to change the string value inside its protoype function:

String.prototype.myfunction=function(){
    this += "a";
    return this;
};

但似乎我无法更改该值.如果我尝试在控制台上运行它,我只会收到错误:Uncaught ReferenceError: Invalid left-hand side in assignment

But as it seems I just can't change the value. If I try to run that on the console I just get the error: Uncaught ReferenceError: Invalid left-hand side in assignment

是否可以更改字符串值?提前致谢

Is it possible to change the strings value? Thanks in advance

推荐答案

我认为这与字符串的不变性以及 this 是一个常量这一事实有关.

I think it has to do with the immutability of strings and the fact that this is a constant.

如果您的示例简单地更改为:

It works just fine if your example is trivially changed to:

String.prototype.myfunction=function(){
    return this + 'a';
};

这篇关于改变字符串值的字符串原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆