variables - Immutable values in javascript - Stack Overflow

I'm currently in the learning process of JavaScript and really get confused about immutable values

I'm currently in the learning process of JavaScript and really get confused about immutable values. What I understand is, when a value is created (stings/numbers/booleans), it can never be changed.

Now my question is, the variable that I assign values, the value of that variable is changeable. I can assign new values to that variable. So why immutability is important?

Also, when I assign a new value to a variable, what happen to the previous value? Does it stay in memory and block some spaces? Does it lost its pointer from that variable? Actually what happen?

Please help me to understand what is the actual use of the concepts "Mutable" and "Immutable" in JavaScript. Thanks in advance.

I'm currently in the learning process of JavaScript and really get confused about immutable values. What I understand is, when a value is created (stings/numbers/booleans), it can never be changed.

Now my question is, the variable that I assign values, the value of that variable is changeable. I can assign new values to that variable. So why immutability is important?

Also, when I assign a new value to a variable, what happen to the previous value? Does it stay in memory and block some spaces? Does it lost its pointer from that variable? Actually what happen?

Please help me to understand what is the actual use of the concepts "Mutable" and "Immutable" in JavaScript. Thanks in advance.

Share Improve this question asked Mar 28, 2016 at 3:55 ProvashProvash 2,0424 gold badges22 silver badges28 bronze badges 2
  • I think this question is similar like below link <br> understatnding immutable variable – Nadimul De Cj Commented Mar 28, 2016 at 3:57
  • Maybe this q&a helps – user5536315 Commented Mar 29, 2016 at 10:17
Add a ment  | 

1 Answer 1

Reset to default 8

What You Seem Confused About

Assignment of a variable has nothing to do with immutable or mutable objects. A variable is simply an alias: it refers to an object in memory. When you change a variable's value, all you've done is tell the piler to stop associating that object with this variable and associate it with another object instead. Assignment does not affect the underlying object - the object never changes, you just no longer have a way to access it anymore.

Usually, when all references to an object are lost, they are garbage collected i.e. the memory allocated for the object is released and the object is lost forever. However, this also has nothing to do with the difference between immutable and mutable objects.

The Real Difference Between Immutable and Mutable Objects

Immutable objects do not modify the object in place (i.e change what it looks like) - they return a new copy of the data with the variables changed.

Mutable objects in Javascript do not return a copy, but let you change the object itself.

I liken it to the difference between array.splice() and array.slice(). splice() will change the original array by removing/inserting elements where needed - slice() will create a new array with just the elements you want. They can be made to do the same things - but one mutates the array in place, the other one creates a copy.

An immutable object is forced to always return a new copy when changed - it will not by itself be changed. A mutable object can - but does not have to - be mutated in place. Primitive types in Javascript are mostly immutable - you cannot change the form of a string once created (if you call replace on the JS string, you will receive a new string with the values you asked for replaced, not the same string with the value changed). Objects are mostly mutable: for instance you can do object[key] = value and change the object everywhere it is referenced.

tl;dr

When you change a mutable object, it is changed everywhere it is referenced. When you change an immutable object, it isn't changed at all - a new object is created, and all the old references to the object will give you the original object with nothing different about it.

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

相关推荐

  • variables - Immutable values in javascript - Stack Overflow

    I'm currently in the learning process of JavaScript and really get confused about immutable values

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信