简体   繁体   English

为什么我在运行此函数时更改了作为参数传递的变量的值?

[英]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.

相关问题 如何根据传递给函数的参数更改变量名? - How can I change a variable name based on a parameter passed to a function? 如何创建将传递的参数视为文字的 function? - How can I create a function which treats a passed parameter as a literal? 反应为什么我在调用作为参数传递的 function 时收到错误“Uncaught TypeError: createTask is not a function”? - REACT Why do I get the error "Uncaught TypeError: createTask is not a function" when calling a function passed as a parameter? 即使我传递了一个参数,为什么我的 doGet(e) function 返回 null 值? - Why is my doGet(e) function returning a null value even though i passed a parameter? 当函数作为参数传入时,如何引用函数的参数? - How do I refer to arguments of a function when the function is passed in as a parameter? 我将一个函数(本身具有一个参数)作为参数传递给另一个函数,以及如何获取该参数 - I passed a function(which itself has a parameter) as a parameter into another function and how to get the parameter 如何检查传递给javascript函数的参数值是否为空? - How can I check if the value of a parameter passed to a javascript function is null? 通过更改传递的 function 参数的值来更改原始变量值 - Change original variable value by changing value of passed function parameter 将变量传递给函数时,如何跟踪变量的值? - How do I keep track of a variable's value when it's passed into a function? 在JS中,当我“ alert()”传递给函数的参数值时,它会打印出[object Object],为什么? - In JS when I 'alert()' the value of an argument passed to the function, it prints out [object Object], why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM