简体   繁体   English

JSON树和内存分配

[英]JSON Tree & Memory Allocation

In JavaScript, if I create an Object, which is my understanding is that this is essentially represented as JSON, and if this object has a handle on its parent object, and the parent has a handle on the child object, how is this held in memory (is it a pointer? Or duplication of JSON). 在JavaScript中,如果我创建一个Object,我的理解是该对象本质上表示为JSON,并且如果此对象在其父对象上具有一个句柄,而父对象在子对象上具有一个句柄,则该对象如何保存内存(是指针吗?还是JSON的重复)。

For example, if I construct a Person Object, like the following, where each Person may have multiple children, and also has a handle on its parent. 例如,如果我构造一个如下的Person对象,则每个Person可能有多个子代,并且在其父代上也有一个句柄。

var Person = function(json, parent) {
  this.name = json.name;
  this.parent = parent;
  this.children = new Array();
  if(json.children) {
    for(var i=0; i < json.children.length; i++) {
      this.children.push(new Person (json. children[i], this));
  }
}

My understanding is that if I change a property of a person, then this will be reflected in the Person, but also the the Parent of any of its children, so this would indicate the actual object is held once in memory and JavaScript uses pointers? 我的理解是,如果我更改一个人的属性,那么这将反映在该人中,也将反映在其任何子项的父项中,因此这将表明实际对象在内存中一次保存并且JavaScript使用指针吗? Am I correct in thinking this? 我认为正确吗?

My fear is this type of structure is expensive to hold in memory, as I may encounter the scenario where I have a few thousand of this type of hierarchical construct, which can go a few levels deep. 我担心这种类型的结构在内存中的存储成本很高,因为我可能会遇到这样的情况:我有数千种这种类型的层次结构,可以深入几个层次。

JSON is a text format for representing javascript objects in a plain text format. JSON是一种文本格式,用于以纯文本格式表示javascript对象。

Javascript objects themselves in Javascript are not JSON at all. Javascript中的Javascript对象本身根本不是JSON。 They are some sort of internal format that is up to the Javascript implementation and is a balance between good runtime performance and efficient size. 它们是某种内部格式,由Javascript实现决定,并且在良好的运行时性能和有效大小之间取得平衡。 When one object contains a reference to another object in Javascript, that is not a copy of the object. 当一个对象包含对Javascript中另一个对象的引用时,这不是该对象的副本。 You can think of it like a pointer to the other object and its likely that it is some sort of pointer internal to the implementation, but that is up to the implementation. 您可以将其视为指向另一个对象的指针,并且它很可能是实现内部的某种指针,但这取决于实现。 The crux is that it works like a pointer. 问题的关键在于它像指针一样工作。

A simple Javascript experiment shows that there's only one copy of the data (thus pointers to a single set of data are used) when you assign an object to multiple variables: 一个简单的Javascript实验显示,当您将一个对象分配给多个变量时,只有一个数据副本(因此使用指向一组数据的指针):

var x = {counter: 2};
var y = x;
x.counter = 10;
console.log(x.counter);   // 10
console.log(y.counter);   // 10

JSON was invented long after Javascript itself as a means of exchanging Javascript data structures between different processes or applications or computers. JSON是在Javascript本身诞生很久之后发明的,它是在不同进程,应用程序或计算机之间交换Javascript数据结构的一种方式。 Since then it has become a common data interchange structure used by many different languages, not just Javascript. 从那时起,它已成为许多不同语言(不仅仅是Java语言)使用的通用数据交换结构。 You can read more about it here: http://www.json.org/ . 您可以在此处了解更多信息: http : //www.json.org/

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

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