简体   繁体   English

有什么方法可以通过JavaScript中的引用来分配变量(不是对象属性)另一个变量的值?

[英]Any way to assign a variable (not an object property) another variable's value by reference in JavaScript?

So, here's what I need: 所以,这就是我需要的:

var a = 5;
var b = a;
a = 6;
alert(b) // I want this to alert 6 not 5

I've seen questions like How does variable assignment work in JavaScript? 我曾经看到过诸如JavaScript中的变量分配如何工作之类的问题 , answers like https://stackoverflow.com/a/509614/1117796 and posts like http://hpyblg.wordpress.com/2010/05/30/simulating-pointers-in-javascript/ . ,例如https://stackoverflow.com/a/509614/1117796之类的答案,以及http://hpyblg.wordpress.com/2010/05/30/simulating-pointers-in-javascript/之类的帖子。 I do NOT want to use objects/hashes/dictionaries or whatever else you want to call them. 我不想使用对象/哈希/字典或其他任何您想调用的对象。 I want this done with plain variables containing plain primitive values. 我希望通过包含普通原始值的普通变量来完成此操作。 Something like the PHP assignment by reference: $b = &$a; 类似于通过引用分配的PHP: $b = &$a; . Is it at all possible? 有可能吗? Anything that comes near to it? 有什么接近的吗?

不,从ECMA Script 5开始这是不可能的。

No its not possible. 没有不可能。 And its a boon that this is not allowed in JavaScript. 而且,这是JavaScript不允许的。

No that isin't possible to pass simple values as reference in JavaScript. 没有,不可能在JavaScript中传递简单值作为参考。

However JavaScript is a flexible language and you could possibly implement something similar using Object.defineProperty by providing setters and getters that will all work with the same variable. 但是JavaScript是一种灵活的语言,您可以使用Object.defineProperty来实现相似的功能, Object.defineProperty是提供设置器和Object.defineProperty器,它们都将使用相同的变量。 That could work for global variables however, there's no way to completely mimic this behaviour. 这可能适用于global变量,但是,没有办法完全模仿这种行为。 For instance, since you can't get a reference to the execution scope of functions, you would not be able to emulate this concept for variables declared inside functions. 例如,由于您无法获得对函数执行范围的引用,因此您将无法在函数内部声明的变量上模仿此概念。

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

相关问题 如何将变量值分配给另一个变量,而不是引用dojo javascript - how to assign a variable value to another variable instead of reference dojo javascript 使用JavaScript将新属性从变量分配给对象的属性 - Assign new Property to an Object's Property From Variable using JavaScript 无法将JSON对象的属性分配给另一个变量 - Unable to assign JSON object's property to another variable AngularJS:为Object属性分配变量引用 - AngularJS : assign variable reference to Object property 从对象获取属性值,并将其分配给javascript中的变量 - Get a property value from an object and assign it to a variable in javascript 有条件地将变量的值分配给对象的属性,该属性可以为null - Conditionally assign the value of a variable to an object's property that may be null 有没有一种方法可以将JavaScript中的变量设置为等于对象的属性作为参考? - Is there a way to set a variable in JavaScript equal to the property of object as a reference? Javascript-将函数分配给变量(引用/值) - Javascript - assign function to variable (reference/value) 如何在没有参考的情况下将对象值分配给变量 - how to assign object value to variable without reference 将“ this”引用分配给javascript中的变量 - Assign 'this' reference to a variable in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM