JSFiddle - React, Tailwind, and code Playground
HTML
Controlled array to restrict length
Babel + JSX
class ControlledArray {
constructor(num) {
this.a = Array(num).fill(0); // Creates new array and fills it with zeros
}
set(arr) {
if (!(arr instanceof Array) || arr.length != this.a.length) {
return false;
}
this.a = arr.slice();
return true;
}
get() {
return this.a.slice();
}
}
$( document ).ready(function($) {
var m = new ControlledArray(3);
alert(m.set('vera')); // fail
alert(m.set(['vera', 'chuck', 'dave'])); // pass
alert(m.get()); // gets copy of controlled array
});