[英]Why I change the value of variable which I passed as parameter when I run this function?
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%"]雷达构造器(radarradar,1) console.log(radarradar.center) #this 然后显示["30%", "50%"]
I checked that Js does not have pass by reference, so I was really confused.我检查了 Js 没有通过引用传递,所以我真的很困惑。 Thanks in advance if you answer this question!!!
如果您回答这个问题,在此先感谢!!!
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.
基元甚至被“包装”在函数和默认属性(如 .toString() 或 .length)的对象处理程序中......不同之处在于基元被视为复制值到新对象中,而常规对象/函数通过它们的引用传递. 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.如果您想从对象获取数据,则有 [property1,propert2] = 雷达析构模式,您可以在其中从对象中提取基元而不是直接修改对象。 there's making a new object - Object.from(radar).
正在制作一个新对象 - Object.from(radar)。
mutation is frowned upon nowadays - as everyone is all about functional style and immutability.现在,mutation 不受欢迎——因为每个人都关心函数式风格和不变性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.