JavaScript我可以将const旋转到输出功能



我现在正在学习javaScript,我在工作时有了一个想法,我可以更改此问题:

const x = this.props.form.getFieldValue('product_gender')
console.log(x)

类似:

this.props.form.getFieldValue('product_gender', x => console.log(x))

如果听起来很愚蠢,请原谅我,但我是JavaScript的新手。谢谢。

第二个代码段是使用"回调"模式,通常仅在函数本身为 asynchronous 时才使用(即,它不会立即返回其结果,而是必须去做一些背景工作,然后最终返回结果)。

因此,仅当相关的特定API支持该模式时才起作用。

如果您只想避免分配变量,那就做:

console.log(this.props.form.getFieldValue('product_gender'));

实际上更改了您的变量定义:

const x = this.props.form.getFieldValue('product_gender', x => console.log(x));

是的,您可以更改getFieldValue功能定义以接受回电并为您运行。您的getFieldValue功能应像以下方式定义:

function getFieldValue(productName, callback){
  // do something with the productName
  const returnValue = calculateYourReturnValue()
  // call the callback function
  callback(returnValue)
  return returnValue
}

如果要在使用该值之前对其进行操作,但不能在JSX中声明const,则定义一个函数来操纵它。这简单明了:

doSomethingWithGender(gender) {
    return gender + "some extra text";
}
render() {
    return <div>
        { this.doSomethingWithGender(this.props.form.getFieldValue('product_gender')) }
    </div>
}

最新更新