Javascript 函数在调用之前执行
Javascript function is executed before it is called
我有一个 javascript 函数抱怨它体内的调用在调用之前不是函数。有人可以帮我解释一下吗?
上下文:我正在使用 Meteor and Collection2,我想使用一个函数在我的模式的不同属性上重用。准确地说,我想做以下事情:
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);
我有一个 javascript 函数抱怨它体内的调用在调用之前不是函数。有人可以帮我解释一下吗?
上下文:我正在使用 Meteor and Collection2,我想使用一个函数在我的模式的不同属性上重用。准确地说,我想做以下事情:
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);