Wyciągi
Bloki instrukcji to bloki bez połączeń wyjściowych. Działają one jak
w językach opartych na tekście.
Bloki instrukcji można zmienić w kod, tak jak w przypadku innych bloków w Blockly
przez zdefiniowanie generatora kodu blokowego.
import {javascriptGenerator} from 'blockly/javascript';
javascriptGenerator.forBlock['custom_block'] = function(block, generator) {
// Collect argument strings.
const fieldValue = block.getFieldValue('MY_FIELD');
const innerCode = generator.statementToCode(block, 'MY_STATEMENT_INPUT');
// Return code.
return 'my code string';
}
Zbierz ciągi argumentów
Wszystkie generatory kodu blokowego wymagają
zbieranie wartości pól i kodu pól
wewnętrzne bloki.
// Collect field values.
const fieldValue = block.getFieldValue('MY_FIELD');
// Collect inner block code strings.
const innerCode = generator.statementToCode(block, 'MY_STATEMENT_INPUT');
Jeśli wielokrotnie odwołujesz się do kodu wewnętrznego bloku, dodaj
buforowania argumentu do bloku.
Zwracane wartości
Typem zwracanym generatora kodu blokowego instrukcji jest ciąg kodu.
return 'my code string';
O ile nie stwierdzono inaczej, treść tej strony jest objęta licencją Creative Commons – uznanie autorstwa 4.0, a fragmenty kodu są dostępne na licencji Apache 2.0. Szczegółowe informacje na ten temat zawierają zasady dotyczące witryny Google Developers. Java jest zastrzeżonym znakiem towarowym firmy Oracle i jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2024-10-15 UTC.
[null,null,["Ostatnia aktualizacja: 2024-10-15 UTC."],[[["Statement blocks in Blockly, similar to statements in text-based languages, lack output connections and function by defining a block-code generator to convert them into code strings."],["Generating code for statement blocks involves collecting values from fields and inner blocks using dedicated functions within the block-code generator."],["Block-code generators for statement blocks return code strings, representing the translated code for the block, and optionally incorporate argument caching for optimized code generation."]]],["Statement blocks, which lack output connections, are converted to code strings using block-code generators. This process involves collecting values from fields and code from inner blocks. The `getFieldValue` method retrieves field values, while `statementToCode` gathers inner block code. If inner block code is used multiple times, argument caching is recommended. Ultimately, a statement block-code generator returns a string representing the generated code.\n"]]