WebMasterCampus
WEB DEVELOPER Resources

Javascript Clone a Object

Learn how clone a Object using Javascript Object.assign


Javascript Clone a Object

In ECMAScript 6 there is Object.assign method, which copies values of all enumerable own properties from one object to another.

Object.assign() Method

<!DOCTYPE html>
<html>
<body>
  <div id="result"></div>

  <script>
    function show(strValue) {
        document.body.appendChild(document.createElement('pre')).innerHTML = strValue;
    }

    var obj = {name: "Martin"};
    var clone = Object.assign({}, obj);

    show (JSON.stringify(obj));
    show (JSON.stringify(clone));

  </script>
</body>
</html>

Read more about Object.assign()

Created with love and passion.