Javascript 基于下划线的迭代器原型未获取值
使用下划线,我迭代一个数据,并添加到一个函数中,添加后,我使用“原型”扩展函数-当我调用该原型时,我没有得到任何值。而且原型根本不可供公众使用 这是我的密码:Javascript 基于下划线的迭代器原型未获取值,javascript,underscore.js,Javascript,Underscore.js,使用下划线,我迭代一个数据,并添加到一个函数中,添加后,我使用“原型”扩展函数-当我调用该原型时,我没有得到任何值。而且原型根本不可供公众使用 这是我的密码: var data = { people : [ {name:'Caring', state:'Tn', price:'100'}, {name:'Mo', state:'Ap', price:'200'}, {name:'af', state:'Jk', price:'33'},
var
data = {
people : [
{name:'Caring', state:'Tn', price:'100'},
{name:'Mo', state:'Ap', price:'200'},
{name:'af', state:'Jk', price:'33'},
{name:'adi', state:'Kl', price:'400'},
{name:'Hu', state:'Ka', price:'600'}
]
},
OrderItem = function(person){
this.person = person
getSummary = function(){
return person.name
+ ' spent '
+ person.price
+ ' in ' + person.state + '.';
}
return {getSummary : getSummary}
};
OrderItem.prototype.data = function(){
return this.person;
}
m = _.collect(data.people, function(value, key, list){
return new OrderItem(value);
})
_.each(m, function(value){
// console.log(value.getSummary()); //works fine
console.log(value.data()); //not working!
})
在u.each方法中,当我调用value.data时,我得到一个错误。如何解决这个问题
您只返回
getSummary
方法。
为什么不使用此
可能重复的
OrderItem = function(person){
this.person = person
getSummary = function(){
return person.name
+ ' spent '
+ person.price
+ ' in ' + person.state + '.';
}
return this;
};