i transformed this:
function mangaelt(obj) { "use strict"; this.mirror = obj.mirror; this.name = obj.name; this.url = obj.url; if (obj.lastchapterreadurl !== undefined) { this.lastchapterreadurl = obj.lastchapterreadurl; this.lastchapterreadname = obj.lastchapterreadname; } else { this.lastchapterreadurl = null; this.lastchapterreadname = null; } this.listchaps = []; if (obj.listchaps !== undefined && obj.listchaps !== null && obj.listchaps !== "null") { if (!isarray(obj.listchaps)) { this.listchaps = json.parse(obj.listchaps); } } this.read = 0; if (obj.read !== undefined && obj.read !== null && obj.read !== "null") { this.read = obj.read; } }
into this:
function mangaelt(obj) { "use strict"; this.mirror = obj.mirror; this.name = obj.name; this.url = obj.url; this.lastchapterreadurl = obj.lastchapterreadurl || null; this.lastchapterreadname = obj.lastchapterreadname || null; this.listchaps = json.parse(obj.listchaps) || []; this.read = obj.read || 0; this.update = obj.update || 1; }
as can see, code more readable , compact. snippet works under normal circumstances fine. thing don't have values in obj
object, so, expect undefined
's here , there. , reason of questions:
- why
json.parse
interpretundefined
string, trowing mdn, "syntax error"undefined
? - should then, i, check before values parsed if value proper string?
- shouldn't json.parse, check whenever value parsed
undefined
, returnundefined
? (this may rise arguments, so, if believe is, ignore question or state i'm wrong train of trough) - if #2 affirmative, adding conditional first snipped should enough, right? or should go function calls mangaelt , make sure
obj.listchaps
array , forgetjson.parse
here?. (this array or pseudo-array in string, , since collaborative project, may have reason this)
for curious may ask, 'what's error getting?' this:
error in event handler 'undefined': unexpected token u syntaxerror: unexpected token u @ object.parse (native) @ new mangaelt (chrome-extension://nhjloagockgobfpopemejpgjjechcpfd/js/mangaelt.js:44:25) @ readmanga (chrome-extension://nhjloagockgobfpopemejpgjjechcpfd/js/background.js:410:24) @ chrome-extension://nhjloagockgobfpopemejpgjjechcpfd/js/background.js:607:9 @ event.dispatchtolistener (event_bindings:356:21) @ event.dispatch_ (event_bindings:342:27) @ event.dispatch (event_bindings:362:17) @ miscellaneous_bindings:165:24 @ event.dispatchtolistener (event_bindings:356:21) @ event.dispatch_ (event_bindings:342:27) event_bindings:346
edit: this existing entries looks like, not generate errors. this scenario motivated question. type of keys same , tested beforehand:
name
stringmirror
stringurl
stringlistchaps
"array" inside stringts
,upts
integers
btw, obj
object, think it's impossible miss. also, chrome extension, don't think that's relevant. complete script here.
undefined
not valid json token. when converting undefined value json, correct practice render null.
Comments
Post a Comment