??? javascript is mutable !!!

There goes another few hours.. :-(
Please dont spread this around and dont say I wrote this after ~20 years of programming.
But JS is kinda mutable ????


var o1,o2
function fChange(arg){
    arg.k += '-new'
    return arg
}

o1 = {k: 'v1'}
o2 = {k: 'v2'}
o2 = fChange(o1)
console.log('1A) o1=' + JSON.stringify(o1))
console.log('1B) o2=' + JSON.stringify(o2))
//---
function fChange2(arg){
    arg = $.extend(true,{},arg)
    arg.k += '-new'
    return arg
}
o1 = {k: 'v1'}
o2 = {k: 'v2'}
o2 = fChange2(o1)
console.log('2A) o1=' + JSON.stringify(o1))
console.log('2B) o2=' + JSON.stringify(o2))

Results in:


1A) o1={"k":"v1-new"}
1B) o2={"k":"v1-new"}
//---
2A) o1={"k":"v1"}
2B) o2={"k":"v1-new"}

Conclusion:
Result 1A) is utterly amazing !
And terrifying !
Is the object o1 passed in by reference ? Yes it appears so.

~Solution:
Can be seen in fChange2(). Use jquery $.extend() to make a (deep or not) value copy.

arg = $.extend(true,{},arg)

And o1 remains unmutated, as we’d want it to be. See result 2A)
Alternative, not needing jquery, is:

arg = JSON.parse(JSON.stringify(arg))

Using the JSON lib also has the advantage that you dont have to handle cases where arg is Array containing objects in which case you’d need to use arg = $.extend(true,[],arg)

Please be silent and enjoy your knowledge !

Advertisements

Mindmapping tools, online freeware.

Want to quickly jot down an idea ? Or an extended overview of something. Having a brainstorm, alone, together ?

Give these free online tools a go, some are quite handy, and once you get a feel for how they work (easy-peasy) you’ll probably use them more and more.

At the moment my preference is for mindmeister/wisemapping for quick ideas and draw.io for more elaborate ideas.

The selected tools are all online (no downloads) freeware, sometimes limited (only 3 charts or something like that) but as -all- of them enable you to export and reimport them you can use them without limit.

Have some great ideas !


http://www.text2mindmap.com/

Text2MindMap This is a great tool, just use enter and (shift)tab and get a beautiful diagram of your idea.

The quickest way to jot down your ideas.

Try it without registration –here —

http://www.text2mindmap.com/


www.mindmeister.com

MindMeister This is a very nice tool, just select a node and use enter and tab to get your idea visualized.

Try it without registration — here —

www.mindmeister.com


http://www.wisemapping.com/http://www.wisemapping.com/
WiseMapping This is a also a great,and sympathetic, tool, just select a node and use enter and tab to get your idea visualized.

Try it without registration –here —

http://www.wisemapping.com/


draw.io
Draw.io This is a bit more advanced tool, can also be used for flow-charts, uml-diagrams, and general all-purpose thingies.

Try it without registration –here —

From the makers of: JGraph. Connecting the dots.

https://www.draw.io/


If you have a gmail or zoho account (both free) you also have powerpointy presentation programs there that you can use. MindMeister and WiseMapping have the upper hand though for mindmapping purposes.


 

More: