-2
function radarconstructor(radar,j){
  let r = radar
  if(j==0){
    r.center[0] = "10%"
  }
  if(j==1){
    r.center[0] = "30%"
  }
  if(j==2){
    r.center[0] = "50%"
  }
  return(r)
}

var radarradar = {
  // shape: 'circle',
  center:["10%","50%"],//put the radar chart in center
  radius: "50%",
  name: {
    textStyle: {
        color: '#fff',
        backgroundColor: '#999',
        borderRadius: 3,
        padding: [3, 5]
    }
  },
  axisLine: {   
    lineStyle: {
        color: 'rgba(100, 100, 100, 0.15)'
    }
},
splitLine: {
    lineStyle: {
        color: 'rgba(255, 255, 255, 0.1)'
    }
},
 
  indicator: [
      { name: 'Dimension1', max: 3},
      { name: 'Dimension2', max: 3},
      { name: 'Dimension3', max: 3},
      { name: 'Dimension4', max: 3},
      { name: 'Dimension5', max: 3},
  ]
}

radarconstructor(radarradar,1) console.log(radarradar.center) #this then shows ["30%", "50%"]

I checked that Js does not have pass by reference, so I was really confused. Thanks in advance if you answer this question!!!

  • 1
    The change to `r.center` is going to persist outside of the method, because you are changing a property of an element passed in. – Taplar Aug 25 '20 at 16:17
  • Does [this answer](https://stackoverflow.com/a/13104500/2299362) help you? – Pipetus Aug 25 '20 at 16:21

1 Answers1

0

so every object gets passed by a reference. primitives even get 'wrapped' in an object handler for functions and default properties like .toString() or .length... the difference is primitives are treated as copied values into new objects, and regular objects/functions get passed around by their reference. so when modifying objects - remember they get passed by reference always.

if you want to get data from an object there is the [property1,propert2] = radar destructoring pattern where you pull out primitives from an object and not modify the object directly. there's making a new object - Object.from(radar).

mutation is frowned upon nowadays - as everyone is all about functional style and immutability.

altruios
  • 986
  • 1
  • 10
  • 29