-1

请有人告诉我为什么下面的第 1 行会引发错误

Uncaught TypeError: Cannot read property 'age' of undefined.

我是 javascript 和骨干网的新手,这个错误对我来说毫无意义。

谢谢

<script>
var Person = Backbone.Model.extend({
    initialize: function(){
        console.log("Person is initialized");
    }
});

var People = Backbone.Collection.extend({
    model: Person,

    initialize: function(){
        console.log("People model is initialized");
    }
});

var person = new Person({age: 12});
var person2 = new Person({age: 15});
var person3 = new Person({age: 12});
var people = new People();
people.add(person);
people.add(person2);

// (1) var ages = _.where(people, {age: 12});
console.log(ages); 
</script>
4

1 回答 1

1

好像你想要这个:

var ages = people.where({age: 12});

UnderscorewhereBackbone Collectionwhere是两个不同的东西。

就像 Fabricio 所说的那样,错误意味着某些代码正在尝试读取未定义的属性。像foo.agewhere foois这样的东西undefined会产生这个错误。

Since you say you are new to javascript... In Chrome dev tools, you can click the error and it will take you to where the error occurred. In this case it takes you to underscore's source code. When an error occurs in a library, 99% of the time (if the library is widely used) it means you are using something wrong, and can check the documentation to see what is going on.

于 2013-03-29T23:38:18.990 回答