JavaScript-ES6函数由于某些奇怪的原因无法工作
我正在设置下面的函数,以便在以后检索它。 但是,由于某些原因,它不起作用:JavaScript-ES6函数由于某些奇怪的原因无法工作,javascript,function,ecmascript-6,Javascript,Function,Ecmascript 6,我正在设置下面的函数,以便在以后检索它。 但是,由于某些原因,它不起作用: constructor(private nativeStorage: NativeStorage) { // I am calling it this way: this.userId = 123; this.getItem("userId", this.userId); // If I replace the shortcut by the below it works fine
constructor(private nativeStorage: NativeStorage) {
// I am calling it this way:
this.userId = 123;
this.getItem("userId", this.userId);
// If I replace the shortcut by the below it works fine
/* this.nativeStorage.getItem("userId).then(
data => this.userId = data,
error => console.error(error)
); */
}
getItem(itemKey, itemValue) {
return this.nativeStorage.getItem(itemKey).then(
data => itemValue = data,
error => console.error(error)
);
}
我相信我在这里遗漏了一些东西,这就是为什么它不起作用的原因您正在将
数据
分配给项目值
,它只是这个.userId的一个副本JS不支持可能实现这一点的按引用传递。相反,您可以使用itemKey
直接分配给类实例,如下所示:
getItem(itemKey) {
return this.nativeStorage.getItem(itemKey).then(
data => this[itemKey] = data, // assign data to this[itemKey] which will be this.userId (in the example above)
error => console.error(error)
);
}
JavaScript不支持按引用调用。它仅通过值arg传递。因此,只能通过对象引用来更新值。在引用部分/注释时,不能使用异步构造函数,因此我将所有异步部分提取到工厂中
我还没有使用ionic的经验,因此您应该将以下内容作为伪代码:
import { NativeStorage } from '@ionic-native/native-storage';
function constructor(public userId: String){ }
//and the factory function
//don't know how exactly how to export stuff in that framework
function createById(userId){
return NativeStorage.getItem('userId')
.then(
userId => new YourClass(userId)
error => console.error(error)
)
}
或者,如果要指定多个属性:
//a utility to resolve with { key: Promise<value> } mappings + pretty much everything you throw at it.
//warning: doesn't resolve promises in nested structures (by design),
//stuff like {key: { foo: Promise<value> }}
//ideally you'd put that in a seperate module since it can be handy in many places.
function resolve(obj){
if(Array.isArray(obj)
return Promise.all(obj);
if(typeof obj === "function")
return Promise.resolve().then(obj);
if(!obj || typeof obj !== "object" || "then" in obj && typeof obj.then === "function")
return Promise.resolve(obj);
var keys = Object.keys(obj);
return Promise.all( keys.map(k => obj[k]) )
.then(values => combine(keys, values));
}
//takes two Arrays, a matching set of keys and values and combines that into an object.
function combine(keys, values){
return keys.reduce((acc, key, index) => {
acc[key] = values[index];
return acc;
}, {});
}
const assignTo = target => source => Object.assign(target, source);
//in this case we use assign all async values outside of the constructor
function constructor(){
this.userId = null;
this.userName = null;
}
function createById(userId){
return resolve({
userId: NativeStorage.getItem('userId'),
userName: NativeStorage.getItem('userName'),
//...
}).then(
assignTo( new YourClass() ),
error => console.error(error)
)
}
希望这有助于向您展示解决问题的不同方法。您希望此函数data=>itemValue=data
做什么?什么不起作用?this.nativeStorage.getItem(itemKey)
是一个承诺?尝试使用然后
和捕获
项值
是一个局部变量。重新分配它在getItem()
之外没有任何效果。您是否尝试隐式更新this.userId
?然后通过@ibrahimmahrir查看答案。JS不支持类似的功能。除此之外,它还有另一个缺陷:this.nativeStorage.getItem()
是异步的。您不知道何时更新this.userId=data
。100毫秒(你几乎没有注意到)就像JS代码的几个小时。除非您处理承诺,否则无论在this.userId
上使用什么代码,都可能会处理旧值。itemKey是一个静态值,我还需要传递,ItemValue@Folky.H你没有使用它,所以没有必要通过它。加上itemKey
就足够了,如果您想要这个值,可以使用this[itemKey]
@Folky.H访问它,这是由于this.nativeStorage.getItem()的异步性质造成的。JS不会等待承诺解决,因此它可以记录更新的值。分配itemValue=data
不会转换为this.userId=data
。这将是不受支持的按引用传递。@Folky.H否。此构造函数不能按您希望的方式工作;不能有异步构造函数。由于这与这个特定的答案无关,让我们在问题本身的评论中继续这个话题。
//a utility to fetch multiple items at once
function getItems(...keys){
return Promise.all( keys.map( key => NativeStorage.getItem(key) ) )
.then(values => combine(keys, values));
}
//and a quick test
getItems("userId", "userName", ...).then(console.log);
//or in the context of the last snippet:
getItems("userId", "userName", ...).then(assignTo( new YourClass() ));