Я делаю функцию конструктора под названием LocalDatastore для моего двигателя игры/физики. Цель состоит в том, чтобы сделать намного проще использовать систему LocalStorage JavaScript. массив для хранения данных). < /p>
Данные отображаются как нулевые в массиве ParsedData, а в LocalStorage нет никаких данных.
Если кто -то может привести меня вправо Направление или может сказать мне, что они считают неправильным, это было бы здорово, спасибо. Файлы JS, чем сделать третий файл JS с именем библиотеки, вставьте мой код, который я разместил здесь. Посмотрите в своей консоли, чтобы увидеть, смогли ли вы успешно получить данные из первого документа)
Пожалуйста, не понижайте Просто потому, что у вас нет времени, чтобы понять мой код. Я дал всю информацию, необходимую вам, чтобы понять, что должен делать мой код, и проблема, с которой у меня есть.
//-----------------------//
let LocalData = new Array(); // local game/player data Array which is used for the LocalDataStore system.
let ParsedData = new Array(); // LocalData is sent here after it is parsed using 'LocalDataStore.get()'.
//-----------------------//
/**********************************************
* You can 'set' data in one html file then access it in another using 'get'.
*
* LocalDataStore.set('key') to set a key name for the data.
*
* LocalDataStore.get('key') to access the data using the key name.
*
* LocalDataStore.add('myData') to add data to the LocalData Array.
*
* LocalDataStore.clear() clears all data.
*
* LocalDataStore.remove('key') to remove specific data.
**********************************************/
let LocalDataStore = function(){
// add data to the LocalData Array.
this.add = function(data){
LocalData.push(data);
console.log("New Data has been added to the 'LocalData' Array");
};
this.set = function(key){
// maybe I should clear localStorage here?
localStorage.clear();
// set a key name for your data in the LocalData Array.
localStorage.setItem(key, JSON.stringify(LocalData));
if(LocalData[0] == null || LocalData[0] == undefined){
console.log("[error@ 'LocalData']: 'no data has been found.'")
}
console.log("'LocalData' has been set with the key name: ["+key+"]");
};
this.get = function(key){
// retrieve your data from LocalData and parse & add it to the ParsedData Array.
let userData = JSON.parse(localStorage.getItem(key));
ParsedData.push(userData);
console.log("[parsed-data: "+userData+"]");
if(ParsedData[0] == null || ParsedData[0] == undefined){
console.log("[error@ 'ParsedData']: 'data-parsing failed or no data has been found.'")
}
console.log("Accessed & Parsed 'LocalData' with the key name: ["+key+"].");
console.log("New Data has been Added to the 'ParsedData' Array.");
};
this.remove = function(key){
// remove specific data from localStorage.
localStorage.removeItem(key)
};
this.clear = function(){
// clear all data from localStorage.
localStorage.clear();
};
};
Это консольная вывод от первого HTML DOC:
New Data has been added to the 'LocalData' Array
'LocalData' has been set with the key name: [pd1]
localStorage
Storage {pd1: '[5]', length: 1}
Это консольная вывод со второго HTML DOC:
[parsed-data: null]
[error@ 'ParsedData']: 'data-parsing failed or no data has been found.'
Accessed & Parsed 'LocalData' with the key name: [pd1].
New Data has been Added to the 'ParsedData' Array.
Подробнее здесь: https://stackoverflow.com/questions/794 ... up-as-null
Localstorage.getItem данные отображаются как null ⇐ Javascript
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Unity3D: предмет успешно найден в инвентаре, но GetItem() все еще имеет значение null?
Anonymous » » в форуме C# - 0 Ответы
- 19 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Unity3D: предмет успешно найден в инвентаре, но GetItem() все еще имеет значение null?
Anonymous » » в форуме C# - 0 Ответы
- 15 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Unity3D: предмет успешно найден в инвентаре, но GetItem() все еще имеет значение null?
Anonymous » » в форуме C# - 0 Ответы
- 12 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Unity3D: предмет успешно найден в инвентаре, но GetItem() все еще имеет значение null?
Anonymous » » в форуме C# - 0 Ответы
- 16 Просмотры
-
Последнее сообщение Anonymous
-