[英]My local storage is not working, I am trying to push values but I got empty
[英]I am trying to use .push() to save multiple values but I got '{}'
此toggle(...vals)
獲取多個單詞並將這些單詞應用於var cur=...
function toggle(...vals) {
var unset = {};
var cur = unset;
return function next(){
// save previous value back at
// the end of the list
vals.push(cur);
return cur;
};
}
var hello = toggle("hello");
console.log(hello());
我期望得到這個結果。
PS C:\Users\leePC\babel\public\src> node practice2_ch2.js
hello
但我得到了這個。
PS C:\Users\leePC\babel\public\src> node practice2_ch2.js
{}
建議的答案說我應該使用cur = vals.shift();
function toggle(...vals) {
var unset = {};
var cur = unset;
return function next(){
// save previous value back at
// the end of the list
vals.push(cur);
cur = vals.shift();
return cur;
};
}
var hello = toggle("hello");
console.log(hello());
令人驚訝的是它有效!
PS C:\Users\leePC\babel\public\src> node practice2_ch2.js
hello
但我仍然不確定為什么這個改變讓我得到了想要的結果......
嘗試使用:
function toggle(...vals) {
var unset = {};
var cur = unset;
return function next(){
// save previous value back at
// the end of the list
vals.push(cur);
return vals;
};
}
var hello = toggle("hello");
console.log(hello());
插入:
function toggle(...vals) {
var unset = {};
var cur = unset;
return function next(){
// save previous value back at
// the end of the list
vals.push(cur);
return cur;
};
}
var hello = toggle("hello");
console.log(hello());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.