Spaces:
Running
Running
File size: 1,835 Bytes
30c32c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
/**
* @fileoverview
* Object representing a Scratch variable.
*/
const uid = require('../util/uid');
const xmlEscape = require('../util/xml-escape');
class Variable {
/**
* @param {string} id Id of the variable.
* @param {string} name Name of the variable.
* @param {string} type Type of the variable, one of '' or 'list'
* @param {boolean} isCloud Whether the variable is stored in the cloud.
* @constructor
*/
constructor (id, name, type, isCloud) {
this.id = id || uid();
this.name = name;
this.type = type;
this.isCloud = isCloud;
switch (this.type) {
case Variable.SCALAR_TYPE:
this.value = 0;
break;
case Variable.LIST_TYPE:
this.value = [];
break;
case Variable.BROADCAST_MESSAGE_TYPE:
this.value = this.name;
break;
default:
console.warn(`Invalid variable type: ${this.type}`);
}
}
toXML (isLocal) {
isLocal = (isLocal === true);
return `<variable type="${this.type}" id="${this.id}" islocal="${isLocal
}" iscloud="${this.isCloud}">${xmlEscape(this.name)}</variable>`;
}
/**
* Type representation for scalar variables.
* This is currently represented as ''
* for compatibility with blockly.
* @const {string}
*/
static get SCALAR_TYPE () {
return ''; // used by compiler
}
/**
* Type representation for list variables.
* @const {string}
*/
static get LIST_TYPE () {
return 'list'; // used by compiler
}
/**
* Type representation for list variables.
* @const {string}
*/
static get BROADCAST_MESSAGE_TYPE () {
return 'broadcast_msg';
}
}
module.exports = Variable;
|