scope - mutation inside a function in javascript - Stack Overflow

n00b question here:Suppose you call a function that updates a number or string like thisvar x = "w

n00b question here:

Suppose you call a function that updates a number or string like this

var x = "well";
var helloify = function(str){
  str += "hello"
};

I'd expect this behavior:

helloify(x);
console.log(x)
\\ "well hello"

But instead I get

\\ "well"

doesn't "+=" change the value of "x"? does this change only persist in the scope of the function but not in the global environment?

thanks!

--confused

n00b question here:

Suppose you call a function that updates a number or string like this

var x = "well";
var helloify = function(str){
  str += "hello"
};

I'd expect this behavior:

helloify(x);
console.log(x)
\\ "well hello"

But instead I get

\\ "well"

doesn't "+=" change the value of "x"? does this change only persist in the scope of the function but not in the global environment?

thanks!

--confused

Share Improve this question asked Nov 1, 2015 at 17:58 aoanthonyaoanthony 411 silver badge2 bronze badges 2
  • "doesn't "+=" change the value of "x"?" Not in this case. It changes the value of str. But, str was only copying the value of x. The two variables are pletely independent and unaware of each other. – Jonathan Lonowski Commented Nov 1, 2015 at 18:01
  • Primitives are passed by value, it means that what you get into a function is actually a copy of the original value outside. – dfsq Commented Nov 1, 2015 at 18:02
Add a ment  | 

2 Answers 2

Reset to default 7

When you call helloify(x); you pass the value of x (a string) not a reference to x.

str += "hello" modifies str and leaves x alone.


NB: Objects are only addressed by reference, so if x had been a reference to an object then you would have modified the single object addressed by both variables. Simple strings are not objects though.

This is because of how parameters are send, because of how strings are handled, and because of what the += operator really does.

Parameters are sent by value, so the function doesn't get the string variable, it gets a copy of the reference to the string object.

Strings are immutable, which means that you will never change a string object1. When you alter a string, that will actually create a new string with the new value.

The str += "hello" is short for str = str + "hello". This means that it will create a new string with the value str + "hello" and put the reference to that string object in the variable str.

As the parameter is not the variable that you sent into the function but a copy, assigning a new string to the parameter won't change the variable that you used in the function call.


1 The Javascript engine might actually change a string object if it knows that the string will never be used any more, i.e. when it can safely do so while keeping the behaviour of immutable strings.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745275783a4620031.html

相关推荐

  • scope - mutation inside a function in javascript - Stack Overflow

    n00b question here:Suppose you call a function that updates a number or string like thisvar x = "w

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信