最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot Set Property ... of undefined --- bizarre - Stack Overflow

programmeradmin1浏览0评论

I'm getting a bizarre error in Chrome... check out the screenshot below.

I define record using object literal syntax.

I try setting the "id" property and get the exception.

I've tried both :

record['id'] = 'wtf';

and also

record.id = 'wtf';

I use this type of syntax all over the place in my script.... what could be going on here ? Is this a bug in Chrome ?

EDIT : I've solved the problem for now, but I'm still not sure why this is happening. I moved the definition of record to occur outside of the if block. Anyone know what could be occurring ? I thought all variable declarations were scoped to the function and therefore this shouldn't be an issue.

I'm getting a bizarre error in Chrome... check out the screenshot below.

I define record using object literal syntax.

I try setting the "id" property and get the exception.

I've tried both :

record['id'] = 'wtf';

and also

record.id = 'wtf';

I use this type of syntax all over the place in my script.... what could be going on here ? Is this a bug in Chrome ?

EDIT : I've solved the problem for now, but I'm still not sure why this is happening. I moved the definition of record to occur outside of the if block. Anyone know what could be occurring ? I thought all variable declarations were scoped to the function and therefore this shouldn't be an issue.

Share Improve this question edited Nov 16, 2010 at 18:15 rvandervort asked Nov 16, 2010 at 18:06 rvandervortrvandervort 3411 gold badge2 silver badges11 bronze badges 1
  • I can't reproduce the error (on Chrome 7 & Firefox on Mac). From the info here and the screen shot, it looks like there's nothing wrong with your code. What is the value of record right before the "erroneous" line? – nickf Commented Nov 16, 2010 at 18:12
Add a comment  | 

2 Answers 2

Reset to default 15

The problem is most likely that dl is less than or equal to zero, so the statement that initializes record doesn't get executed. From your indentation, it looks like you intended for both statements to be part of the if block, but with no braces, the record['id'] = 'wtf'; statement gets executed no matter what.

By moving the variable initialization outside the if statement, you forced it to happen in any case and moved the assignment inside the if block (which, I'm assuming is what you wanted).

Probably a better way to solve it is adding braces like this:

if (dl > 0) {
    var record = {};

    record.id = 'wtf';
}

Unless you really do want to initialize record in both cases.

You are correct about variable declarations being scoped to the function, but the assignment doesn't happen until you get to that point in the code. record was in scope, but it still had its default value of undefined because you hadn't assigned anything to it yet.

Works for me, no reason it shouldn't work. Are you sure it's referring to that exact line? what if you alert(record) before you set it? Have you tried to debug it yet?

发布评论

评论列表(0)

  1. 暂无评论