Relevant DOM operations for developing shopping carts with JavaScript (3)
This section mainly encapsulates various operations in the shopping cart, such as counting the number of products, updating and obtaining local data, etc., to facilitate code management.
First, check whether the local data contains the specified object (commodity) based on the id
Secondly, update the local data through the array object and obtain the total quantity of the commodity
Finally , update the local data according to the product identification, and then obtain the local data.
Comments:
JSON.stringify
Converts a JavaScript value to a JavaScript Object Notation (Json) string.
Syntax: JSON.stringify(value [, replacer] [, space])
value: is a required field. It is the object you input, such as array, class, etc. replacer: This is optional. It is divided into 2 methods, one is an array, and the second is a method.
Case 1: When replacer is an array, we can know through subsequent experiments that it is related to the first parameter value. Generally speaking, the serialized results are represented by key-value pairs. Therefore, if the value of the second parameter exists in the first one at this time, then the value of the second parameter will be used as the key, and the value of the first parameter will be represented as value. If it does not exist, it will be ignored.
Situation 2: When replacer is a method, it is very simple, that is, each serialized object (remember, each one) is passed into the method for processing.
space: What is used as the separator.
1) If omitted, the displayed value will have no separator and will be output directly.
2) If it is a number, then it defines how many characters to indent. Of course, if it is greater than 10, the default is 10, because the maximum value is 10.
3) If it is some escape characters, such as "\t", indicating a carriage return, then it will have one carriage return per line.
4) If it is just a string, append these strings to each line when outputting the value. Of course, the maximum length is also 10 characters.
#Create a server.js file and put the above JavaScript code into it.
Later called from the HTML page to achieve the function module effect.