如何从引用属性确定父对象

时间:2022-06-08 16:55:34
var object = {foo: 'bar'};

Does JavaScript have a way to determine that object.foo is a property of object from inside a function to which object.foo is passed? In other words, is it possible to write a func that could do this:

JavaScript是否有办法确定object.foo是object.foo传递给的函数内部的对象的属性?换句话说,是否可以编写一个可以执行此操作的函数:

getSource(object.foo) === object

2 个解决方案

#1


6  

No, there's no back-reference like that in JavaScript. What gets passed into a function is a value -- 5, "foo", an object reference, etc. There's no information on that value that tells you the value came from an object property, much less which object's property.

不,在JavaScript中没有类似的反向引用。传递给函数的是值 - 5,“foo”,对象引用等。没有关于该值的信息告诉您来自对象属性的值,更不用说对象的属性。

#2


2  

For variables, Javascript uses "pass by value", in other words, when object.foo gets passed to any function, what the function will receive is 'bar', not a reference to object.foo:

对于变量,Javascript使用“按值传递”,换句话说,当object.foo传递给任何函数时,函数将接收的是“bar”,而不是对object.foo的引用:

var object = {foo: 'bar'};
function by_value(v) {
    v = v + v
}
document.write(by_value(object.foo)) // Writes 'bar'

In other words, once you have passed object.foo to any function, there's no way to know that this comes from object

换句话说,一旦你将object.foo传递给任何函数,就无法知道这来自于对象

However, if you pass an object, the value of that variable will be a reference to that object: (Note: this is not true "pass by reference", see @t-j-crowder's comment):

但是,如果传递一个对象,该变量的值将是对该对象的引用:(注意:这不是真的“按引用传递”,请参阅@ t-j-crowder的注释):

function as_reference(o) {
    o.foo = o.foo + o.foo
}
document.write(by_reference(object)) // Writes 'barbar'

Meaning as a cheap work-around, you can store a "reference" to your object in the object as well:

意思是廉价的解决方法,你也可以在对象中存储一个“引用”:

var object = {foo: 'bar'};
object.foo = {val: object.foo, parent: object}
function do_something(v) {
    console.log(v.val);   // foo
    console.log(v.parent) // object
}
do_something(object.foo)

Helpful?

#1


6  

No, there's no back-reference like that in JavaScript. What gets passed into a function is a value -- 5, "foo", an object reference, etc. There's no information on that value that tells you the value came from an object property, much less which object's property.

不,在JavaScript中没有类似的反向引用。传递给函数的是值 - 5,“foo”,对象引用等。没有关于该值的信息告诉您来自对象属性的值,更不用说对象的属性。

#2


2  

For variables, Javascript uses "pass by value", in other words, when object.foo gets passed to any function, what the function will receive is 'bar', not a reference to object.foo:

对于变量,Javascript使用“按值传递”,换句话说,当object.foo传递给任何函数时,函数将接收的是“bar”,而不是对object.foo的引用:

var object = {foo: 'bar'};
function by_value(v) {
    v = v + v
}
document.write(by_value(object.foo)) // Writes 'bar'

In other words, once you have passed object.foo to any function, there's no way to know that this comes from object

换句话说,一旦你将object.foo传递给任何函数,就无法知道这来自于对象

However, if you pass an object, the value of that variable will be a reference to that object: (Note: this is not true "pass by reference", see @t-j-crowder's comment):

但是,如果传递一个对象,该变量的值将是对该对象的引用:(注意:这不是真的“按引用传递”,请参阅@ t-j-crowder的注释):

function as_reference(o) {
    o.foo = o.foo + o.foo
}
document.write(by_reference(object)) // Writes 'barbar'

Meaning as a cheap work-around, you can store a "reference" to your object in the object as well:

意思是廉价的解决方法,你也可以在对象中存储一个“引用”:

var object = {foo: 'bar'};
object.foo = {val: object.foo, parent: object}
function do_something(v) {
    console.log(v.val);   // foo
    console.log(v.parent) // object
}
do_something(object.foo)

Helpful?