0

I am trying to declare a variable with the value returned by a function:

var myValue = hot.getSettings();

However, the value of myValue keeps changing when the return value of getSettings() change. How can I define myValue as the current value of the getSettings() function, and save that value in myValue, even if getSettings change?

user2806026
  • 787
  • 3
  • 10
  • 24
  • What exactly does `getSettings` return…? – deceze Sep 14 '16 at 07:45
  • do you return value from hot.getSettings() and did you print what does it return? – Aatif Bandey Sep 14 '16 at 07:46
  • getSettings is the getSettings-function declared in Handsontable. It saves the current settings of a handsontable instance. What I'm trying to do is save the current instance - but what i've saved keeps changing. http://docs.handsontable.com/0.16.1/Core.html#getSettings – user2806026 Sep 14 '16 at 07:53
  • You want to make a copy of the object: http://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object – deceze Sep 14 '16 at 08:20
  • @user2806026 You're downvoting answers which you think are not answering your question, but not upvoting the answer which you have marked as the accepted answer... – Force444 Sep 22 '16 at 13:31

3 Answers3

0

Your problem it that hot.getSettings(); return a reference not a copy of variable So you just need to clone that variable

try this :

var value = hot.getSettings();
var myValue = $.extend(true, {}, value);

I hope it will help.

R.K.Saini
  • 2,678
  • 1
  • 18
  • 25
0

If you're already using Handsontable, you can use its helpers to do that real quick:

var snapshot = Handsontable.helper.deepClone(hot.getSettings());

This way you'll create a deep clone of the settings object, which should not be referencing the original one anymore.

JS.Ziggle
  • 81
  • 5
-1

if you want to conserve different values that the function hot.getSettings() might return you need to store them somewhere,

for ex an Array:

var arr = []
var myValue = arr[0]

arr.push(hot.getSettings());//push returns the length , and you could use that for naming a key in an object if needed 
maioman
  • 18,154
  • 4
  • 36
  • 42
  • That is exactly what I started out doing, but the value in the array keeps changing.. – user2806026 Sep 14 '16 at 08:07
  • @user2806026 if you return something that points to another-something , whenever another-something changes also something will change; if this is your situation the easiest solution is [making a copy of the return object](http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript?noredirect=1&lq=1) – maioman Sep 14 '16 at 08:19