Difference between revisions of "Storage (Class)"

From 8x9craft
Jump to: navigation, search
 
Line 1: Line 1:
 
[[Category:APIs]]
 
[[Category:APIs]]
There are two classes you can use to store objects: <code>localStorage</code> (permanent) and <code>sessionStorage</code> (temporary).
+
ストレージは2つのクラスで使うことができます。一つはlocalStorage(恒久的)でもうひとつはsessionStorage(一時的)です。
  
 
<source lang=js>
 
<source lang=js>
// Save a value to storage.
+
// ローカルストレージに値を保存
 
var obj = {
 
var obj = {
 
   name: 'Hakkun',
 
   name: 'Hakkun',
Line 11: Line 11:
 
localStorage.setItem('pet', value);
 
localStorage.setItem('pet', value);
  
// Retrieve a value from storage.
+
// ローカルストレージから値を取得
 
var value = localStorage.getItem('pet');
 
var value = localStorage.getItem('pet');
 
if (value) {
 
if (value) {
Line 19: Line 19:
  
 
{| class="example"
 
{| class="example"
! Field
+
! フィールド
! Description
+
! 説明
 
|-
 
|-
 
| setItem(key : string, value : string) : void
 
| setItem(key : string, value : string) : void
| method must first check if a key/value pair with the given key already exists in the list associated with the object.
+
| 記憶領域内に与えられたキーで値を格納します。
 
|-
 
|-
 
| getItem(key : string) : string
 
| getItem(key : string) : string
| method must return the current value associated with the given key. If the given key does not exist in the list associated with the object then this method must return empty string.
+
| キーで格納している値を返します。もし、キーがなければ空のstringを返します。
 
|-
 
|-
 
| keys(): String[]
 
| keys(): String[]
| return copy of keys.
+
| 領域にある全てのキーを返します。
 
|-
 
|-
 
| length(): Number
 
| length(): Number
| return size of keys.
+
| 格納している値の数を返します。
 
|-
 
|-
 
| removeItem(key: string) : void
 
| removeItem(key: string) : void
| method must cause the key/value pair with the given key to be removed from the list associated with the object, if it exists. If no item with that key exists, the method must do nothing.
+
| 記憶領域から与えられたキーに紐つく値を削除します。
 
|-
 
|-
 
| clear(): void
 
| clear(): void
| method must atomically cause the list associated with the object to be emptied of all key/value pairs, if there are any. If there are none, then the method must do nothing.
+
| 記憶領域から全ての格納データを削除します。
 
|}
 
|}

Latest revision as of 04:56, 14 July 2017

ストレージは2つのクラスで使うことができます。一つはlocalStorage(恒久的)でもうひとつはsessionStorage(一時的)です。

// ローカルストレージに値を保存
var obj = {
  name: 'Hakkun',
  score: 42
}
var value = JSON.stringify(obj);
localStorage.setItem('pet', value);

// ローカルストレージから値を取得
var value = localStorage.getItem('pet');
if (value) {
  var obj = JSON.parse(value);
}
フィールド 説明
setItem(key : string, value : string) : void 記憶領域内に与えられたキーで値を格納します。
getItem(key : string) : string キーで格納している値を返します。もし、キーがなければ空のstringを返します。
keys(): String[] 領域にある全てのキーを返します。
length(): Number 格納している値の数を返します。
removeItem(key: string) : void 記憶領域から与えられたキーに紐つく値を削除します。
clear(): void 記憶領域から全ての格納データを削除します。

Navigation menu

Internal error

Jump to: navigation, search
[db762603d0bad38f351d25e1] 2024-03-28 21:00:10: Fatal exception of type "Error"

Navigation menu