0

I am tyring to do something like this:

this.slides = {$('#Page_1') : null,
               $('#Page_2') : null,
               $('#Page_3') : null};

Why am I doing this, or what advantage do I gain?
I dont want to use jquery selectors throughout class.


A Solution:

this.slides = [{"key": $('#Page_1'), "value": null},
               {"key": $('#Page_2'), "value": null},
               {"key": $('#Page_3'), "value": null}];

Limitation:

The problem with this approach is that you have to iterate through the whole object each time you want to approach this. You should use the id as identifier, this is much more efficient. – Christoph

11
  • this is nonsense - the key is meant to be the identifier, you can't use an object for that. Also i cannot think of a usecase where this might be useful. Commented Oct 16, 2012 at 16:51
  • @Asad i guess i thought becuase each object was uniquely identifyable this might be possible. Commented Oct 16, 2012 at 16:52
  • @Christoph: no need to be so harsh, OP is clearly just trying to learn. Commented Oct 16, 2012 at 16:54
  • correct, but a key is supposed to be for data retrieval, not for the data itself. the value (which you are setting to null) is supposed to hold the data, in this case your DOM element. Commented Oct 16, 2012 at 16:56
  • 1
    You should probably be using data() and not an object. Commented Oct 16, 2012 at 17:02

4 Answers 4

3

No, keys must be literals. you could use a property of your object.

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

2 Comments

I edited my answer, I guess a literal would be the best way to put it, since a key could also be an integer? Im not sure what the best term is.
I think literal is just fine, I'm not sure either - probably the ES-spec has the answer but I'm too lazy too look it up -.-
2

If your processing is based on the this.slides content, why not use the id as key ? since they are also meant to be "unique" otherwise you would break the DOM. So i would suggest something like

this.slides = {'#Page_1':null,'#Page_2':null,'#Page_3':null};

And a very light modification of your processing of this.slides

1 Comment

i want to use objects so i dont need to use jquery selectors throughout class. thankyou though.
1

Why don't you try a structure like this:

this.slides = [{"key": $('#Page_1'), "value": null},
               etc.];

Or does that break your need for using an object?

Of course, when iterating it, you'd have to use logic like:

for (var i = 0; i < this.slides.length; i++) {
    var key = this.slides[i].key;
    var value = this.slides[i].value;
}

7 Comments

@DanKanze Cool, well I just added a quick note on how to access each item (I'm sure you would've known how to anyways)
Leveraging the underscore library can make dealing with this type of schema easier to deal with.
underscorejs.org specifically, pluck, find, etc. If you need to look an object up by some sort of filter. The collection methods are very useful.
@DanKanze The problem with this approach is that you have to iterate through the whole object each time you want to approach this. You should use the id as identifier, this is much more efficient. See my answer for more detail.
@Christoph It completely depends on what the OP is using this for. Either approach could be correct, as we don't know exactly how this is being used or the way it's being accessed. Either solution is perfectly fine and could work better than the other.
|
1

You cannot use objects as keys, only plain literals. I'd suggest a structure like this:

this.slides = { 'Page_1' : $('#Page_1'),
                'Page_2' : $('#Page_2')
                'Page_3' : $('#Page_3')};

This way you can use the id of the elements to easily access the according jQuery-Object. (You can omit the quotes for the keys.)

slides.Page_1
// or
slides['Page_1']

now gives you the according jQuery Object.

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.