-1

Is there any online tool/ js library which generates immutable expressions with Object.assign and array methods by taking input as mutable expression? Ex:

var obj = {a:1};

//Input
var obj2 = obj;

//Output
var obj2 = Object.assign({}, obj);

EDIT: I found immutablejs makes it easy to work with immutable objects. But the syntax looks a bit difficult to adapt.

Pranay Kumar
  • 2,139
  • 1
  • 15
  • 15
  • what should they generate. The reducers are basically your business logic. – wheeler Sep 04 '17 at 18:08
  • Hello. Your question is off-topic and will likely be closed soon. I recommend that you complete our [tour](https://stackoverflow.com/tour) and read our [help center](https://stackoverflow.com/help) to understand what is on-topic for this site. – Robert Columbia Sep 04 '17 at 18:10
  • What do you mean by "expression"? Can you give some examples of expected input and output? – Bergi Sep 04 '17 at 18:11
  • 1
    The `obj2` in the output is just as mutable as the `obj2` in the input. To me this sounds more like a duplicate of [What is the most efficient way to deep clone an object in JavaScript?](https://stackoverflow.com/q/122102/1048572) – Bergi Sep 04 '17 at 18:24
  • First off, this question is on topic and indicative that this is not your daddy's JavaScript. Kindly don't close this topic b/c I have a pertinent answer. – slevy1 Sep 04 '17 at 18:31
  • @slevy1 I'm pretty sure that your answer is not what the OP was looking for. And that's exactly why it's going to be closed - it's totally unclear what the desired result is or why a tool that generates code would be needed for it. – Bergi Sep 04 '17 at 18:50
  • @Bergi I would strongly urge you to kindly let this topic stay open as it captures the imagination and inspires one to dig more into modern JavaScript. Let's all enjoy the learning experience and let the truth lead us where it may. – slevy1 Sep 04 '17 at 18:59
  • @slevy1 Sorry, but StackOverflow is the wrong platform for imagination. It's not a forum, it's Q&A platform and requires precise questions. Sure you still can get inspired by clever answers and learn a great deal, but not like this. – Bergi Sep 04 '17 at 19:03

1 Answers1

1

So as it turns out what could not be achieved in yesteryear is possible now, i.e. creating an immutable object in JavaScript and you don't need anything other than a modern version of JavaScript that supports this feature. Here's an example:

var obj = {"a":1};

//Input
var obj2 = obj;

Object.freeze( obj2 );

console.log( Object.isFrozen( obj2 ) );

// silence is golden; can't do this:
obj2.foo = "fooey";
console.log( obj2 );

See more helpful info at MDN

slevy1
  • 3,797
  • 2
  • 27
  • 33
  • 1
    "*silence is golden*" - you should always [`"use strict"` mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode), so that it'll shout in your face :-) – Bergi Sep 04 '17 at 18:49
  • 1
    I was expecting some transpiler like tool which generates code. But this is cool. – Pranay Kumar Sep 04 '17 at 18:56