Spread operator copies Symbol property from one object into another

by Konstantin Rouda

JavaScript

;(function (){
	"use strict";
  
  /*Using local scope Symbol as object private key*/
	const secretProp = Symbol("secret property");
  
  const obj = {
      firstName: "Jack",
      lastName: "Black", 
      age: 33,
  /*First method to set property name as Symbol*/
      [secretProp]: "secret value"
  };

  /*another method to set property name as Symbol*/
  ///obj[secretProp] = "777";
  
  
  /*copy all properties from one object into another*/
  const newObj = {...obj}
  
  console.log(newObj);
  
  // we should not get this value, as Symbol("secret property"); is not enumerable and should not be copied
  document.body.textContent = "newObj[secretProp]: " + newObj[secretProp];
  
})();