Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
374 views
in Technique[技术] by (71.8m points)

node.js - Javascript object when setting property all properties with same name are set

I've only recently started working with javascript and NodeJs. I'm facing a simple problem that I am finding difficult to find answers to online. I'm initialising an object holding a number of products each product then has a price and availability. When I try and set the price of one product, it sets the price of all the products rather than just the one i wanted to set. What am I doing wrong?

var ProdStruct = {
  'price' : 0,
  'available' : 0,
};
var Prods = {
  '1' : ProdStruct,
  '2' : ProdStruct,
  '3' : ProdStruct,
  '4' : ProdStruct,
  '5' : ProdStruct,
  '6' : ProdStruct,
  '6' : ProdStruct,
  '7' : ProdStruct
};
Prods['6']['price'] = 99;
console.log(Prods);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have only one ProdStruct object, which you're referring to seven times. That is, you have something like this in memory:

                                        +??????????????+
ProdStruct??????????????+?+?+?+?+?+?+??>|   (object)   |
                       / / / / / / /    +??????????????+
                       | | | | | | |    | price: 0     |
                       | | | | | | |    | available: 0 |
                       | | | | | | |    +??????????????+
         +??????????+  | | | | | | |
Prods???>| (object) |  | | | | | | |
         +??????????+  | | | | | | |
         | 1        |??+ | | | | | |
         | 2        |????+ | | | | |
         | 3        |??????+ | | | |
         | 4        |????????+ | | |
         | 5        |??????????+ | |
         | 6        |????????????+ |
         | 7        |??????????????+
         +??????????+

Modifying the state of that one object (changing price) modifies its state; doesn't matter which of the seven references you use to get to it to make the change or to look at the result, it's just one object.

You'd need to make a copy of the object to get the result you expect. One way is to use Object.assign:

var ProdStruct = {
  'price' : 0,
  'available' : 0,
};
var Prods = {
  '1' : Object.assign({}, ProdStruct),
  '2' : Object.assign({}, ProdStruct),
  '3' : Object.assign({}, ProdStruct),
  '4' : Object.assign({}, ProdStruct),
  '5' : Object.assign({}, ProdStruct),
  '6' : Object.assign({}, ProdStruct),
  '6' : Object.assign({}, ProdStruct),
  '7' : Object.assign({}, ProdStruct)
};
Prods['6']['price'] = 99;
console.log(Prods);
.as-console-wrapper {
  max-height: 100% !important;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...