简体   繁体   English

Javascript在函数内部使对象文字等于{},

[英]Javascript make object literal equal {} inside in the function,

How to understand this output? 如何理解此输出?

Why data.value is still equal to 'value'? 为什么data.value仍然等于'value'?

 function funcA(data) { data.key = 'data'; } function funcB(data) { data = {}; data.value = 'data'; } var data = { key: 'key', value: 'value' }; funcA(data); funcB(data); console.log(data.key); //data console.log(data.value); //value 

Please help. 请帮忙。 I do not know how to understand this question. 我不知道如何理解这个问题。 Thanks for helping in advance. 感谢您的提前帮助。

You have to understand how objects are passed in JavaScript functions. 您必须了解如何在JavaScript函数中传递对象。 It's a reference to an object in memory that's being passed. 它是对要传递的内存中对象的引用。 Once you do data = {} in funcB , the function's local copy of the reference data now points to a new, empty object, not the original location of data as defined outside of the function. 一旦你这样做data = {}funcB ,该函数的局部参考的复制 data现在指向一个新的空对象,而不是原来的位置data作为函数的定义之外。 Then the line data.value = 'data' simply modifies that local, empty object, not the original object. 然后,行data.value = 'data'只是修改该本地的空对象,而不是原始对象。

This data = {}; data = {}; just updates the local parameter, not the original variable. 只是更新本地参数,而不是原始变量。

If JS was a "pass by reference" language, that would work, but it isn't. 如果JS是一种“按引用传递”语言,那会起作用,但事实并非如此。 You're passing a "reference type" , but are passing it "by value" , so only the value (the object's reference) gets updated to a new instance. 您传递的是“引用类型” ,但传递的是“按值” ,因此只有值(对象的引用)被更新为新实例。

Arguments are passed by value, but the names of arguments are not the same as the values. 参数是通过值传递的,但是参数的名称与值不同。 In your funcB() you have a couple things going on that are confusing because the names are all the same. funcB()您会发生一些令人困惑的事情,因为名称都是相同的。

function funcB(data) {
  // the **name** `data` is not the same as the data you declared earlier
  // even though they both point to the same object
  // this inner data shadows the outer data

  data = {};
  // now you set the inner `data` name to point to a new object and you have two datas
  // one outside that you set with var data and this one. Since you can only have one
  // data name in the scope the inner one wins.
  data.value = 'data';
  // changing data here only changes the inner one.
}

It's easier to see if you use distinct names: 如果您使用不同的名称,则更加容易:

 function funcB(inner_data) { // the name inner_data and data both point to the same object console.log(inner_data === data) // now inner_data points to something else inner_data = {}; inner_data.value = 'data'; } var data = { key: 'key', value: 'value' }; funcB(data); console.log(data.key); //data console.log(data.value); //value 

The confusion happens because if you didn't name your argument data you could still access the outer data from the function. 之所以会造成混乱,是因为如果您不命名参数data您仍然可以从该函数访问外部data For example: 例如:

 function funcB(inner_data) { // the name inner_data and data both point to the same object console.log(inner_data === data) // but since you're not shadowing the outer data with // a new name in this scope you can still reach outer data data = {}; data.value = 'data'; } var data = { key: 'key', value: 'value' }; funcB(data); console.log(data.key); //data console.log(data.value); //value 

first you should already know that when u assign an object to a variable this variable will hold a reference to this object's data in the memory 首先,您应该已经知道,当您将对象分配给变量时,此变量将在内存中保存对该对象数据的引用

a reference is nothing but a memory location so that we know where the data of the object is stored 引用不过是存储位置,因此我们知道对象数据的存储位置

js has something called Call by sharing , i had troubles understating it but after a little search my conclusion was : js有一个叫做“通过共享调用”的东西,我很难低估它,但是经过一番搜索之后,我的结论是:

if you just changed property of an object inside a function scope , it will affect the variable globally (outside the function scope) cause u changed the values of the object that this variable is referencing not the reference itself , like your funcA 如果您只是在函数作用域内更改了对象的属性 ,则它将全局影响该变量(在函数作用域之外),这是因为u更改了该变量所引用的对象的值,而不是引用本身,例如funcA

unless you re-assigned a new object to this variable, in this case you are changing where your variable referencing , its now referencing to something else so any change you do to it inside the function will not affect the variable values outside the function , funcB doesn't affect the variable globally (outside of the function scope) because it creates a new local object with a new reference which is scoped to the function only 除非您为该变量重新分配了一个新对象 ,否则在这种情况下,您将更改变量引用的位置,该变量现在引用的是其他内容,因此您在函数内部对其所做的任何更改都不会影响函数funcB之外的变量值不会全局影响变量(在函数作用域之外),因为它会创建一个具有新引用的新本地对象,该引用仅作用于该函数

as for primitive types they will always be pass by value , so any changes made to them will only affect the variable in the scope of the function making the change 至于原始类型,它们将始终按值传递,因此对它们进行的任何更改只会影响进行更改的函数范围内的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM