Bản khảo sát nghiên cứu: Cho chúng tôi biết trải nghiệm của bạn khi sử dụng Blockly
Bắt đầu khảo sát
Bảng sao kê
Khối câu lệnh là các khối không có kết nối đầu ra. Những hành động này giống như
câu lệnh bằng ngôn ngữ dựa trên văn bản.
Giống như tất cả các khối trong Blockly, khối câu lệnh có thể được chuyển thành mã
bằng cách xác định trình tạo mã khối.
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';
}
Thu thập chuỗi đối số
Tất cả các trình tạo mã khối cần
thu thập giá trị của các trường và thu thập mã của
khối bên trong.
// Collect field values.
const fieldValue = block.getFieldValue('MY_FIELD');
// Collect inner block code strings.
const innerCode = generator.statementToCode(block, 'MY_STATEMENT_INPUT');
Nếu tham chiếu mã của một khối bên trong nhiều lần, bạn nên thêm
lưu đối số vào bộ nhớ đệm vào khối của bạn.
Giá trị trả về
Loại dữ liệu trả về của trình tạo mã khối câu lệnh là một chuỗi mã.
return 'my code string';
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2024-10-15 UTC.
[null,null,["Cập nhật lần gần đây nhất: 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"]]