javascript - Detect invalid use of object members -
i have evaluator returns object saying whether or not show elements on page , contains on 50 individual evaluations. such, not easy eyeball detect invalid uses of object members. here simplified example of issue find tool me address:
var simpleexample = { evaluationset1: { foo: true }, evaluationset2: { bar: true }, showbutton: null }; //accidental mis-use simpleexample.showbutton = simpleexample.evaluationset1.bar && simpleexample.evaluationset2.bar; in above example, showbutton not evaluated , false because evaluationset1.bar not defined on object. correct syntax be:
simpleexample.showbutton = simpleexample.evaluationset1.foo && simpleexample.evaluationset2.bar; is there tool can detect kind of invalid use-case? have tried jshint , jslint, neither seems able flag it.
use queue. if evaluation sequential , required, process evaluationset1 evaluationsetn, else throw error. is, provide no option access property of simpleexample. provide function can access evaluationset1 can begin process, , can complete process @ evaluationsetn.
main thread
const worker = new worker("worker.js"); function handlemessageevent(event) { if (event.data) { console.log("evaluation true", event.data) } else { console.log("evaluation false", event.data) } } worker.addeventlistener("message", handlemessageevent); worker.postmessage("evaluation"); worker thread
function handleworkerevent(event) { let bool = true; if (event.data !== "evaluate") { bool = false; self.postmessage(bool); return; } (let prop of object.keys(simpleexample)) { let curr = simpleexample[prop]; if (curr) { let keys = object.keys(curr); if (keys.length) { (let key of keys) { if (!curr[key]) { bool = false; break; } } } } else { bool = false; break; } } self.postmessage(bool); } self.addeventlistener("message", handleworkerevent); data.js
const simpleexample = { evaluationset1: { foo: true }, evaluationset2: { bar: true }, showbutton: null };
Comments
Post a Comment