gpt4 book ai didi

javascript - 在字符串变量上调用 toUppercase()

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:42 24 4
gpt4 key购买 nike

我是初学者,刚刚成功地解决了我的代码问题。我很高兴我找到了它,但是我花了很长时间。我希望了解它发生的原因。

这是有问题的原始代码。假设变量 [nextAlpha] 已经被赋予了一个字符串值:

nextAlpha.toUpperCase();

通过一些创造性的测试,我能够确定是那条线导致了问题。我想也许它实际上并没有更新变量 [nextAlpha] 的值。我尝试了这个,它奏效了:

nextAlpha = nextAlpha.toUpperCase();

我已经省略了其余的代码,但假设 [var = nextAlpha] 已经在我的脚本顶部声明,我认为这意味着“全局”。有了这些信息,我认为只需调用变量上的方法就足够了。为什么这不像我执行额外步骤(重新)将其分配给原始 [nextAlpha] 字符串时那样将字符串“更新”为大写?

最佳答案

toUpperCase 将转换后的字符串作为新对象返回 - 它不会对 nextAlpha 执行转换。

来自 Mozilla 引用:

The toUpperCase method returns the value of the string converted to uppercase. toUpperCase does not affect the value of the string itself.

reference

关于javascript - 在字符串变量上调用 toUppercase(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26771944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com