scratch-vm/src/engine/variable.js

55 lines
1.3 KiB
JavaScript
Raw Normal View History

/**
* @fileoverview
* Object representing a Scratch variable.
*/
const uid = require('../util/uid');
2017-04-17 19:42:48 -04:00
class Variable {
/**
* @param {string} id Id of the variable.
* @param {string} name Name of the variable.
* @param {(string|number)} type Type of the variable, one of "" or "list"
2017-04-17 19:42:48 -04:00
* @param {boolean} isCloud Whether the variable is stored in the cloud.
* @constructor
*/
constructor (id, name, type, isCloud) {
this.id = id || uid();
2017-04-17 19:42:48 -04:00
this.name = name;
this.type = type;
2017-04-17 19:42:48 -04:00
this.isCloud = isCloud;
switch (this.type) {
case Variable.SCALAR_TYPE:
this.value = 0;
break;
case Variable.LIST_TYPE:
this.value = [];
break;
default:
throw new Error(`Invalid variable type: ${this.type}`);
}
2017-04-17 19:42:48 -04:00
}
toXML () {
return `<variable type="${this.type}" id="${this.id}">${this.name}</variable>`;
}
/**
* Type representation for scalar variables.
* @const {string}
*/
static get SCALAR_TYPE () {
return '';
}
/**
* Type representation for list variables.
* @const {string}
*/
static get LIST_TYPE () {
return 'list';
}
2017-04-17 19:42:48 -04:00
}
module.exports = Variable;