1

I am a bit of a noob when it comes to this. I am using JSON to define a class/object called product and I am not sure how to pass arguments to access a certain part of that object. Pardon me for not being able to explain it better. Hopefully my code sample will help:

var product = {
    p14lb: {
        one: 10.00,
        two: 20.00,
        three: 30.00,
        four: 40.00,
        five: 50.00,
        six: 60.00,
        colorCharge: 2.00
    },
    p20lb: {
        one: 20.00,
        two: 30.00,
        three: 40.00,
        four: 50.00,
        five: 60.00,
        six: 70.00,
        colorCharge: 1.00
    },
    getPrice: function (productQty,colorQty) {

        // I can get the values like this        
        return this.p20lb.one * productQty;

        // but i'd like to be able to pass in the value of what object I want to access
        // so instead of hard coding p20lb.one, 'p20lb' would be an argument I could pass
        // to getPrice, and 'one' would also be an argument

    }
};

This is how I am accessing it currently, works great.

var myProduct = product.getPrice(144,2);

As I said in the comment in the above code sample, I'd like to not hard code p20lb.one, 'p20lb' would be an argument I could pass to getPrice, and 'one' would also be an argument. So maybe something like this:

getPrice: function (productQty,colorQty,productName,tier) {

    return this.productName.tier * productQty;

}

var myProduct = product.getPrice(144,2,'14lb','two');

But that is no go, I get undefined no matter what I do. I'm clearly a noob when it comes to this. Is what I'm trying to do possible? If so, can you point me in the right direction? Thanks!

3
  • 2
    BTW: What you refer to is called "object literal", not JSON. JSON is a very strict string format that can be understood by JavaScript's eval(). Your sample is valid JavaScript, but definitely not JSON-compliant. Commented Jan 21, 2011 at 0:49
  • +1 Cool, thanks for that! I was using this as reference (see #2) phpied.com/3-ways-to-define-a-javascript-class Commented Jan 21, 2011 at 1:06
  • Ah, and now that I read the comments on the article I linked, someone also pointed that out to the author, too. Good to know! Commented Jan 21, 2011 at 1:09

3 Answers 3

4

Try this[productName][tier] instead. In JavaScript, the dot notation and bracket notation are identical. That is, obj.a is the same as obj['a'].

Sign up to request clarification or add additional context in comments.

Comments

1

Simple.

getPrice: function (productQty,colorQty,productName,tier) {

    return this[productName][tier] * productQty;

}

Objects can be accessed in array notation. Don't forget to wrap it in a try-catch block to handle the cases where a certain key combination does not exist.

Comments

0

Can you just do:

var colorQty = product.p20lb.one;
var myProduct = product.getPrice(144, colorQty);

getPrice: function (productQty, colorQty) {
    return productQty * colorQty;
}

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.