function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Benjamin DoctorsBenjamin Doctors 

THIS IS A DISGUSTING, TEMPORARY HACK

Hi Devs,
I'd finished the Input Data Using Forms unit from Aura Components Basics module and in some part they use a JSON.parse(JSON.stringify()) and call it a DISGUSTING TEMPORARY HACK.
My problem is that I don't understand why are they using that.
As far as I know, JSON.parse(JSON.stringify()) clones an object (use with care since can give strange results sometimes), but I don't get why clone when you already have the object.
Even worse, I removed that line of code and everything seems to work perfectly.
 This is all the explanation in the unit: 
"The “disgusting hack” works around a similar issue with references. To see the issue, change the line to remove the two JSON calls, and test the app. You’ll see what the problem is quickly enough. We’ll remove it in the next unit, and so won’t explain further."
The misterious hack is in the helper.js file.
({
    createExpense: function(component, expense) {
        let theExpenses = component.get("v.expenses");
        // Copy the expense to a new object
        // THIS IS A DISGUSTING, TEMPORARY HACK
        let newExpense = JSON.parse(JSON.stringify(expense));
        theExpenses.push(newExpense);
        component.set("v.expenses", theExpenses);
    }
})
Please, help me understand this hack.

Regards,

Benja
 
Dale JamesDale James
Code with comments marking temporary hacks are usually good code, as opposed to most below-average code where everything is a temporary hack that ends up not being so temporary. Dinar Recaps (https://www.dinarrecaps.org/)