admin管理员组文章数量:1417691
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 ofx
. 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
2 Answers
Reset to default 7When 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.
本文标签: scopemutation inside a function in javascriptStack Overflow
版权声明:本文标题:scope - mutation inside a function in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745275783a2651176.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论