0

Using underscore, I iterating a data, and adding in to a function, after i added, i am extending the function using 'prototype' - when i call that prototype i am not getting any value. and the prototype not available for public use at all..

here is my code :

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!
})

In the _.each method, when i call the value.data I am getting error. how to fix this?

DEMO HERE

1

1 Answer 1

1

You're only returning the getSummary method. Why not use this?

jsfiddle

OrderItem = function(person){
    this.person = person
    getSummary = function(){
      return  person.name
        + ' spent '
        + person.price
        + ' in ' + person.state + '.';
    }

    return this;
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.