在 Javascript 中打印数组(或对象)的处理程序/引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13685706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
print the handler/reference of an array (or object) in Javascript
提问by Daniele B
Possible Duplicate:
How can I get the memory address of a JavaScript variable?
可能的重复:
如何获取 JavaScript 变量的内存地址?
Is there a way, in javascript, to print the reference of an array?
有没有办法在javascript中打印数组的引用?
What I want to do is to check if two arrays have the same reference, and it can be done like this post suggests: How to check if two vars have the same reference?
我想要做的是检查两个数组是否具有相同的引用,并且可以像这篇文章建议的那样完成:如何检查两个变量是否具有相同的引用?
But is it possible to print the reference, if for instance I'm working with an array?
但是是否可以打印引用,例如,如果我正在使用数组?
Example
例子
var Array1=["hi"];
var Array2=["hello"];
var thesame = Array1==Array2;
the same value is false
.
相同的值是false
。
But can I print he Array1 reference with somethink like window.alert(@Array1);
in javascript?
但是我可以像window.alert(@Array1);
在 javascript 中那样打印他的 Array1 引用 吗?
--UPDATE --
- 更新 -
What I exactly want is the actual address space being referenced.
我真正想要的是被引用的实际地址空间。
采纳答案by Asad Saeeduddin
Javascript implementations are only standardised in so much as they follow the ECMA spec. The intricacies of memory storage could differ by browser, and are not made accessible to JS.
Javascript 实现仅在遵循 ECMA 规范的情况下才标准化。内存存储的复杂性可能因浏览器而异,并且 JS 无法访问。
As far as your question of whyis concerned: JS is a lightweight scripting language. It makes sense to delegate memory management and optimization tasks to the platform, whereas it would require unnecessary hoop jumping to expose an interface for this to you.
至于你为什么关心的问题:JS 是一种轻量级的脚本语言。将内存管理和优化任务委托给平台是有意义的,而它需要不必要的跳环来向你公开一个接口。
回答by Minko Gechev
If you want to see the value of some kind of pointer to the referenced type (array, object) - you can't.
如果您想查看某种指向引用类型(数组、对象)的指针的值 - 您不能。
For example in, Perl you have a scalar type which is a reference to something. When you print this scalar you can get something like a string representation of the pointer to the value. There's no analogy of that kind of scalar in JavaScript.
例如,在 Perl 中,您有一个标量类型,它是对某物的引用。当您打印此标量时,您可以获得类似于指向该值的指针的字符串表示形式。JavaScript 中没有这种标量的类比。
You can only check whether two references are pointing to the same thing or not.
您只能检查两个引用是否指向同一事物。
As in all languages with GC, in JavaScript you cannot be sure that the same object will be with the same memory pointer along the program execution (it can be moved in the heap). As I mentioned, you don't have an abstraction of pointer as build-in type in the language. So showing the current reference may be is not very good idea, because it may be dynamical (depending on the architecture of the engine and especially the GC).
与所有具有 GC 的语言一样,在 JavaScript 中,您无法确定同一个对象在程序执行过程中将具有相同的内存指针(它可以在堆中移动)。正如我所提到的,您没有将指针抽象为语言中的内置类型。所以显示当前参考可能不是一个好主意,因为它可能是动态的(取决于引擎的架构,尤其是 GC)。