javascript - Cannot Set Property ... of undefined --- bizarre -
i'm getting bizarre error in chrome... check out screenshot below.
i define record using object literal syntax.
i try setting "id" property , exception.
i've tried both :
record['id'] = 'wtf';
and also
record.id = 'wtf';
i use type of syntax on place in script.... going on here ? bug in chrome ?
edit : i've solved problem now, i'm still not sure why happening. moved definition of record occur outside of if block. know occurring ? thought variable declarations scoped function , therefore shouldn't issue.
the problem dl
less or equal zero, statement initializes record
doesn't executed. indentation, looks intended both statements part of if
block, no braces, record['id'] = 'wtf';
statement gets executed no matter what.
by moving variable initialization outside if statement, forced happen in case , moved assignment inside if block (which, i'm assuming wanted).
probably better way solve adding braces this:
if (dl > 0) { var record = {}; record.id = 'wtf'; }
unless want initialize record
in both cases.
you correct variable declarations being scoped function, assignment doesn't happen until point in code. record
in scope, still had default value of undefined
because hadn't assigned yet.
Comments
Post a Comment