Javascript函数没有被调用?范围问题?
我正在尝试对json对象执行验证。但似乎根本没有调用方法ispQuantityInvalid方法 即使poQuantity不为null、空或0,也无法通过验证。 似乎根本没有调用ispQuantityInvalid方法。我没有看到警报。 我错过了什么? 这似乎是一个范围问题。但我使用的是箭头函数,应该不会导致这个问题,对吗 变化 ispoQuantityInvalid==>元素、索引、数组=>{ 警报//没有接到呼叫。 返回此.isEmptyelement.poQuantity; } 到 ispoQuantityInvalid=元素,索引,arra=>{ 警报//没有接到呼叫。 返回此.isEmptyelement.poQuantity; }Javascript函数没有被调用?范围问题?,javascript,typescript,validation,ecmascript-6,scope,Javascript,Typescript,Validation,Ecmascript 6,Scope,我正在尝试对json对象执行验证。但似乎根本没有调用方法ispQuantityInvalid方法 即使poQuantity不为null、空或0,也无法通过验证。 似乎根本没有调用ispQuantityInvalid方法。我没有看到警报。 我错过了什么? 这似乎是一个范围问题。但我使用的是箭头函数,应该不会导致这个问题,对吗 变化 ispoQuantityInvalid==>元素、索引、数组=>{ 警报//没有接到呼叫。 返回此.isEmptyelement.poQuantity; } 到 isp
第一个是返回要调用的函数的函数。因此,内部函数仅可用,但从未调用。if语句看起来像if function e,i,a{},它不是false,因此执行if语句体。=>元素,索引,数组=>{//do work}是无效语法。如果想要一个arrow函数而不传递任何东西,可以使用=>{//do work}如果想要传递一个参数,可以使用foo=>{//do work}。如果需要传递多个函数,请使用foo,bar=>{//do work}@volt谢谢,它正在调用函数,但验证仍然失败。isEmpty不是一个函数IspQuantityInvalid=…正在创建全局变量,但this.IspQuantityInvalid可能正在引用其他变量。确保变量的作用域正确无误,但它没有执行OP所要求的操作是正确的。函数现在被调用,但仍然失败。isEmpty不是一个函数,只需在isEmpty之前删除this。或者,如果您处于对象上下文中,并且需要this(在我再次查看您的代码后,情况似乎是这样),请将这一切绑定到您正在调用的函数中:if this.selectedRows.somethis.ispoQuantityInvalid.bind this
selectedRows = [{
"itemNumber": "",
"lineNumber": 1,
"description": "PUMP,COMPLETE, P/N E12LYTFS-137",
"uom": "",
"poQuantity": "1",
"recievedQuantity": "3",
"isMatched": false,
"p2PLineItemId": "168512",
"quantityUnitPrice": "1",
"buyerItemNumber": null
}];
ispoQuantityInvalid = () => (element, index, array) => {
alert() //not getting called.
return this.isEmpty(element.poQuantity);
}
console.log(this.selectedRows)
if (this.selectedRows.some(this.ispoQuantityInvalid)) {
console.log('po qty is null or empty') //always gets called.
return;
}
isEmpty = (value) => {
return (value == null || value.length === 0 || value === '' || value == 0);
}