Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
c.UNIT = "cm";
// TypeError: cannot modify constant property UNIT
c._index = 1;
// TypeError: cannot modify private property _index
console.log(c._index);
// TypeError: cannot access to private property _index
c.setIndex(2);
console.log(c.getIndex());
// 2
console.log(Object.keys(c)); // private variables are not enumerated
// ["radius", "UNIT"]
// change the private convention for all models
Model.prototype.conventionForPrivate = key => key.startsWith("#");
// remove the constant convention specifically for Circle
Circle.conventionForConstant = () => false;
// Private and constant conventions have been changed
c._index = 3;
c.UNIT = "cm";
console.log(c._index, c.UNIT); // no more errors
// 3 "cm"
import { Model, ObjectModel } from "objectmodel";
function log(msg, errors) {
console.log(msg);
errors.forEach(error => {
console.log(error);
});
document.write(
[msg, ...errors.map(e => e.message)].join("<br>→ ") + "<br><br>"
);
}
Model.prototype.errorCollector = function(errors) {
log("Global error collector caught these errors:", errors);
};
const Student = ObjectModel({
name: String,
course: ["math", "english", "history"],
grade: Number
}).assert(
student => student.grade >= 60,
"should at least get 60 to validate semester"
);
new Student({ name: "Joanna", course: "sleep", grade: 0 });
Student.errorCollector = function(errors) {
log("Student model error collector caught these errors:", errors);