研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
值
值块是具有输出连接的块。它们的作用类似于
表达式)。
与 Blockly 中的所有块一样,您可以通过以下方法将值块转换为代码字符串
定义区块码生成器。
import {javascriptGenerator, Order} from 'blockly/javascript';
javascriptGenerator.forBlock['custom_block'] = function(block, generator) {
// Collect argument strings.
const fieldValue = block.getFieldValue('MY_FIELD');
const innerCode = generator.valueToCode(block, 'MY_VALUE_INPUT', Order.ATOMIC);
// Return code.
return ['my code string', Order.NONE];
}
收集参数字符串
所有块码生成器都需要
收集字段的值和收集
内部代码块。
// Collect field values.
const fieldValue = block.getFieldValue('MY_FIELD');
// Collect inner block code strings.
const innerCode = generator.valueToCode(block, 'MY_VALUE_INPUT', Order.ATOMIC);
如果您多次引用内部代码块的代码,则应添加
参数缓存到块中。
返回代码
值块码生成器的返回值类型是一个数组,其中第一个
值是一个代码字符串,第二个值优先。
return ['my code string', Order.NONE];
优先级控制何时在块码字符串周围添加括号。
如需详细了解相关信息,请参阅圆括号文档
如何控制何时添加括号。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-09-09。
[null,null,["最后更新时间 (UTC):2024-09-09。"],[[["Value blocks in Blockly have output connections and function like expressions in text-based languages, requiring code generation for integration into projects."],["Code generation for value blocks involves defining a block-code generator that collects values from fields and inner blocks to create a code string."],["Block-code generators for value blocks return an array containing the generated code string and its precedence, which determines the use of parentheses in the final code."],["For optimal performance, consider argument caching if referencing the code of an inner block multiple times within your block definition."]]],[]]