值
值區塊是與輸出連線的區塊。它們就像是價值 (也就是
運算式)。
就像 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];
在區塊程式碼字串前後加上括號時,優先順序可控管。
詳情請參閱括號說明文件。
如何控制加上括號的時機
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-09-09 (世界標準時間)。
[null,null,["上次更新時間: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."]]],[]]