แบบสำรวจการวิจัย: บอกให้เราทราบเกี่ยวกับประสบการณ์การใช้งาน Blockly
เริ่มแบบสำรวจ
ช่องตัวแปร
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ฟิลด์ตัวแปรจะจัดเก็บสตริงเป็นค่า และสตริงเป็นข้อความ
ค่าคือรหัสของตัวแปร ส่วนข้อความคือชื่อของตัวแปร
ฟิลด์ตัวแปร

ฟิลด์ตัวแปรที่เปิดเครื่องมือแก้ไข

ฟิลด์ตัวแปรในบล็อกที่ยุบ

การสร้างวิดีโอ
ไม่ได้พิมพ์
JSON
{
"type": "example_variable_untyped",
"message0": "variable: %1",
"args0": [
{
"type": "field_variable",
"name": "FIELDNAME",
"variable": "x"
}
]
}
JavaScript
Blockly.Blocks['example_variable_untyped'] = {
init: function() {
this.appendDummyInput()
.appendField('variable:')
.appendField(new Blockly.FieldVariable('x'), 'FIELDNAME');
}
};
พิมพ์
JSON
{
"type": "example_variable_typed",
"message0": "variable: %1",
"args0": [
{
"type": "field_variable",
"name": "FIELDNAME",
"variable": "x",
"variableTypes": ["Number", "String"],
"defaultType": "Number"
}
]
}
JavaScript
Blockly.Blocks['example_variable_typed'] = {
init: function() {
this.appendDummyInput()
.appendField('variable:')
.appendField(new Blockly.FieldVariable(
'X',
null,
['Number', 'String'],
'Number'
), 'FIELDNAME');
}
};
ตัวสร้างตัวแปรจะรับชื่อตัวแปรที่ไม่บังคับ เครื่องมือตรวจสอบที่ไม่บังคับ อาร์เรย์ประเภทตัวแปรที่ไม่บังคับ และประเภทเริ่มต้นที่ไม่บังคับ
- ชื่อตัวแปรควรเป็นสตริง ชื่อนี้จะเป็นชื่อของ
ตัวแปรเริ่มต้นที่ฟิลด์มี หากเป็นค่า Null หรือไม่ได้กำหนด ระบบจะสร้างชื่อที่ไม่ซ้ำกัน
- ประเภทตัวแปรควรเป็นอาร์เรย์ของสตริง ซึ่งจะบอกฟิลด์ว่าฟิลด์สามารถเก็บตัวแปรประเภทใดได้ (เช่น ตัวแปรประเภทใดที่จะ
เพิ่มลงในเมนูแบบเลื่อนลง) หากเป็นค่า Null หรือไม่ได้กำหนด ระบบจะยอมรับตัวแปรทุกประเภท (และเพิ่มลงในเมนูแบบเลื่อนลง)
- ประเภทเริ่มต้นควรเป็นสตริง ระบบจะใช้ค่านี้เมื่อสร้างโมเดลตัวแปรเริ่มต้นของฟิลด์
หากมีการกำหนดค่านี้ ควรระบุค่านี้ในอาร์เรย์ประเภทตัวแปร หากเป็นค่า Null หรือไม่ได้กำหนด ค่านี้จะเริ่มต้นเป็นสตริงว่าง ซึ่งหมายความว่าตัวแปรเริ่มต้นจะมีการกำหนดประเภทแบบยืดหยุ่น
→ ดูข้อมูลเพิ่มเติมเกี่ยวกับการพิมพ์อย่างเข้มงวดได้ที่การตรวจสอบประเภท
การเรียงอันดับ
JSON
JSON สำหรับฟิลด์ตัวแปรจะมีลักษณะดังนี้
{
"fields": {
"FIELDNAME": {
"id": "QJD^+@[RVIwbLSZoDb:V"
}
}
}
โดย FIELDNAME
คือสตริงที่อ้างอิงช่องตัวแปร และ
ค่าคือรหัสของตัวแปรที่ช่องอ้างอิง
หากใช้ฟิลด์นี้ในกล่องเครื่องมือ คุณยังระบุชื่อ
และประเภท (ไม่บังคับ) ได้โดยตรง เนื่องจากจะไม่มีตัวแปรให้
อ้างอิง
{
"fields": {
"FIELDNAME": {
"name": "my_variable",
"type": "string"
}
}
}
XML
XML สำหรับฟิลด์ตัวแปรมีลักษณะดังนี้
<field name="VARIABLE" id="QJD^+@[RVIwbLSZoDb:V" variabletype="">name</field>
- แอตทริบิวต์
name
ของโหนดมีสตริงที่อ้างอิงฟิลด์ตัวแปร
- แอตทริบิวต์
id
ของโหนดมีรหัสของตัวแปรที่ฟิลด์
อ้างอิง
- แอตทริบิวต์
variabletype
ของโหนดมีประเภทของตัวแปร
variabletype
เป็นไปตามกฎเดียวกันกับพารามิเตอร์ประเภทเริ่มต้นของตัวสร้าง
- ข้อความด้านในของโหนดคือชื่อของตัวแปร ค่าข้อความด้านใน
เป็นไปตามกฎเดียวกันกับพารามิเตอร์ชื่อตัวแปรของตัวสร้าง
การสร้างเครื่องมือตรวจสอบตัวแปร
ค่าของฟิลด์ตัวแปรคือสตริง ดังนั้นเครื่องมือตรวจสอบใดๆ ต้องยอมรับสตริงและ
ส่งคืนสตริง null
หรือ undefined
ต่อไปนี้คือตัวอย่างเครื่องมือตรวจสอบที่ยอมรับเฉพาะตัวแปรที่กำหนดไว้ล่วงหน้าบางตัวเป็น
ตัวเลือก คุณจะต้องกำหนดตัวแปรเหล่านี้ด้วยฟังก์ชัน
Workspace.getVariableMap().createVariable
เมื่อโหลดพื้นที่ทำงาน
function(newValue) {
var validIds = ['Worf', 'Riker', 'Picard'];
if (validIds.indexOf(newValue) == -1) {
return null;
}
return newValue;
}

เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-05 UTC
[null,null,["อัปเดตล่าสุด 2025-08-05 UTC"],[[["\u003cp\u003eVariable fields store a variable's ID as its value and the variable's name as its text, enabling blocks to interact with variables.\u003c/p\u003e\n"],["\u003cp\u003eYou can specify the variable's name, type, and validation rules during field creation to control its behavior.\u003c/p\u003e\n"],["\u003cp\u003eVariable fields can be serialized to JSON or XML for saving and loading block configurations.\u003c/p\u003e\n"],["\u003cp\u003eValidators can be used to restrict the variables a field can accept, ensuring data integrity.\u003c/p\u003e\n"],["\u003cp\u003eVariable fields can be pre-populated with specific variable names and types within the toolbox.\u003c/p\u003e\n"]]],[],null,["# Variable fields\n\nA variable field stores a string as its value, and a string as its text. The\nvalue is an ID of a variable, while the text is the name of a variable.\n\n#### Variable field\n\n#### Variable field with editor open\n\n#### Variable field on collapsed block\n\nCreation\n--------\n\n### Untyped\n\n### JSON\n\n {\n \"type\": \"example_variable_untyped\",\n \"message0\": \"variable: %1\",\n \"args0\": [\n {\n \"type\": \"field_variable\",\n \"name\": \"FIELDNAME\",\n \"variable\": \"x\"\n }\n ]\n }\n\n### JavaScript\n\n Blockly.Blocks['example_variable_untyped'] = {\n init: function() {\n this.appendDummyInput()\n .appendField('variable:')\n .appendField(new Blockly.FieldVariable('x'), 'FIELDNAME');\n }\n };\n\n### Typed\n\n### JSON\n\n {\n \"type\": \"example_variable_typed\",\n \"message0\": \"variable: %1\",\n \"args0\": [\n {\n \"type\": \"field_variable\",\n \"name\": \"FIELDNAME\",\n \"variable\": \"x\",\n \"variableTypes\": [\"Number\", \"String\"],\n \"defaultType\": \"Number\"\n }\n ]\n }\n\n### JavaScript\n\n Blockly.Blocks['example_variable_typed'] = {\n init: function() {\n this.appendDummyInput()\n .appendField('variable:')\n .appendField(new Blockly.FieldVariable(\n 'X',\n null,\n ['Number', 'String'],\n 'Number'\n ), 'FIELDNAME');\n }\n };\n\nThe variable constructor takes in an optional variable name, an optional\n[validator](#creating_a_variable_validator), an optional array of variable\ntypes, and an optional default type.\n\n- The **variable name** should be a string. This will be the name of the initial variable the field holds. If it is null or undefined a unique name will be generated.\n- The **variable types** should be an array of strings. This tells the field what types of variables the field can hold (i.e. what types of variables to add to the dropdown). If it is null or undefined, all variable types will be accepted (and added to the dropdown).\n- The **default type** should be a string. This will be used when creating the field's initial variable model. If this is defined, its should be included in the variable types array. If it is null or undefined this value defaults to an empty string, meaning the initial variable will be flexibly typed.\n\n→ For more information on strict typing, see\n[Type Checks](/blockly/guides/create-custom-blocks/type-checks).\n\nSerialization\n-------------\n\n### JSON\n\nThe JSON for a variable field looks like so: \n\n {\n \"fields\": {\n \"FIELDNAME\": {\n \"id\": \"QJD^+@[RVIwbLSZoDb:V\"\n }\n }\n }\n\nWhere `FIELDNAME` is a string referencing a variable field, and\nthe value is the ID of the variable the field references.\n\nIf you are using this field in the toolbox, you can also specify the name\nand (optional) type directly, since there will be no variable available to\nreference. \n\n {\n \"fields\": {\n \"FIELDNAME\": {\n \"name\": \"my_variable\",\n \"type\": \"string\"\n }\n }\n }\n\n### XML\n\nThe XML for a variable field looks like so: \n\n \u003cfield name=\"VARIABLE\" id=\"QJD^+@[RVIwbLSZoDb:V\" variabletype=\"\"\u003ename\u003c/field\u003e\n\n- The node's `name` attribute contains a string referencing a variable field.\n- The node's `id` attribute contains the ID of the variable the field references.\n- The node's `variabletype` attribute contains the type of the variable. The `variabletype` follows the same rules as the constructor's default type parameter.\n- The node's inner text is the name of the variable. The inner text value follows the same rules as the constructor's variable name parameter.\n\nCreating a variable validator\n-----------------------------\n\n| **Note:** For information on validators in general see [Validators](/blockly/guides/create-custom-blocks/fields/validators).\n\nA variable field's value is a string, so any validators must accept a string and\nreturn a string, `null`, or `undefined`.\n\nHere's an example of a validator that only accepts some predefined variables as\noptions. These variables would need to be defined with the\n[`Workspace.getVariableMap().createVariable`](/blockly/reference/js/blockly.ivariablemap_interface.createvariable_1_methodsignature)\nfunction when the workspace is loaded. \n\n function(newValue) {\n var validIds = ['Worf', 'Riker', 'Picard'];\n if (validIds.indexOf(newValue) == -1) {\n return null;\n }\n return newValue;\n }"]]