views:

35

answers:

1

I'm at a loss here. I have this class I've created that takes arguments in object form:

new Widget('id_of_element', { option1: 'foo', option2: 'bar' });

However, the second argument isn't being seen as a hash, but as an object, so I can't apply default settings to if one is not set:

initialize: function (element, options) {
    this.options = $H({ option1: 'something', option2: 'else', option3: 'hello', option4: 100 }).update(options);
}

I need the vales from the argument 'options' to be converted into a Hash, so I can use the update() function. I can't find anything in the Prototype framework that will cast an object as a Hash.

What's strange is, on this.options.inspect(), all the correct values show up, but when I call it:

alert(this.options.option1);
// or 
alert(this.options['option1']);

... they come back as undefined. Why would the Hash#inspect method find these values, but are still undefined? Am I missing some substantial understanding of some type?

+1  A: 

There's no such thing as casting in JavaScript. If you want a Hash then you're going to need to create one, which you are already doing.

From the Prototype docs, it appears you need to obtain the values using the Hash's methods:

alert(this.options.get("option1"));
Tim Down
Whether or not I can cast is irrelevant. I should be able to call these values. If Hash#inspect can see them as they should be, why can't I extract the values for use?
Mike
Updated my answer.
Tim Down
Thanks. I knew about the Hash#get method, but I was really hoping the variables could be accessed more simply - much like some of the Element methods which return an x/y and left/top data pair that is accessible this way
Mike