[英]Javascript function is executed before it is called
我有一個javascript函數抱怨它的主體內的調用不是被調用之前的函數。 有人可以幫我解釋一下嗎?
上下文:我正在使用Meteor和Collection2 ,我想使用一個函數在Schema的不同屬性上重用。 確切地說,我想執行以下操作:
function foo (autoVal, self){
if(something){
return autoVal;
}else{
return self.unset();
}
}
export const myCollection = new Mongo.Collection('myCollection');
const Schema = new SimpleSchema({
my_field:{
type:Boolean,
autoValue: foo(false,this),
}
});
myCollection.attachSchema(Schema);
保存並運行Meteor時,它不會啟動,並且出現以下錯誤消息:
TypeError: self.unset is not a function
我覺得我缺少有關如何調用或執行javascript函數的信息,有人可以指出為什么會這樣嗎?
嘗試這個:
function foo (autoVal, self){
if(something){
return autoVal;
}else{
return self.unset();
}
}
export const myCollection = new Mongo.Collection('myCollection');
const Schema = new SimpleSchema({
my_field:{
type:Boolean,
autoValue() {
return foo(false, this);
}
}
});
myCollection.attachSchema(Schema);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.