views:

458

answers:

3

i am trying not to repeat the selector and get to its children via a the same objects parentElment declared variable.

I tried:

testimonialsBelt={
 parentElment:$(".testimonialsCntnr ul"),
 childrenElem:this.parentElment.children().length
}

I also tried:

testimonialsBelt={
 parentElment:$(".testimonialsCntnr ul"),
 childrenElem:$("testimonialsBelt.parentElment").children().length
}

but i keep on getting a undefined when calling alert(testimonialsBelt.childrenElem).

  1. is there anyway to get the jquery object with object literals?

  2. What is the rule? when can i use this and when must i have the full path? (in this case testimonialsBelt.parentElment).

i am trying to have all these variables in one object called testimonialsBelt. i know i can do this with loose javaScript. Thanks

A: 

The problem is you cannot reference parentElement because it is not in scope, and technically doesn't exist anyway since it is part of an object assignment.

You can get around your scope issue by returning the object you want from an anonymous function:

testimonialsBelt = (function() {
 var parentElement = $('.testimonialsCntnr ul');
 return { parentElement: parentElement, childrenElem: parentElement.children().length }
}());
Matt
This is useless considering that `childrenElem` is now static, especially considering that many uses of JavaScript deal with dynamic DOM constructs. If a static value is needed, `var childrenElem = testimonialsBelt.parentElement.children().length;` is far more appropriate.
Justin Johnson
i am trying to have all these variables in one object called testimonialsBelt. i know i can do this with loose javaScript.. anyway thx
adardesign
"Loose javascript"? As in.. not taking advantage of function-scope? You can technically write it this way, however you will be calling the jQuery selector twice, so I do not recommend it: var testimonialsBelt = {parentElement: $('.testimonialsCntnr ul'), childrenElem: $('.testimonialsCntnr ul').children().length };
Matt
Stop trolling, Justin, I clearly said it is not the recommended way.
Matt
A: 

In object literals, you can only use this to refer to the object that you're declaring inside of a function. Try the following:

var testimonialsBelt = {
    parentElment: $(".testimonialsCntnr ul"),
    childrenElem: function() {
        return this.parentElment.children().length;
    }
};

The difference in calling childrenElem is that instead of using alert(testimonialsBelt.childrenElem), you would instead have alert(testimonialsBelt.childrenElem()).

Otherwise, this refers to the current scope that you are in (typically window if you are declaring the object literal as a global).

Addressing your edit: I'm not sure what you mean by "loose javascript," but I assume you mean as simple as possible. In which case, you can try the following, although I'm not a big fan of the method. It's more verbose, but is easy to understand.

var testimonialsBelt = {
    parentElment: $(".testimonialsCntnr ul")
};
testimonialsBelt.childrenElem = parentElment.children().length;

This gives you an object where childrenElem is static (it doesn't change) and avoids calling $(".testimonialsCntnr ul") twice. However, if you expect $(".testimonialsCntnr ul").children() to change, then you will need to use my first example.

Justin Johnson
This is unnecessarily inefficient. OP would now have to call testimonialsBelt.childrenElem() just to pull the property, which in turn has to call parentElement.children().This would only be acceptable if the UL is adding list items dynamically throughout the script's lifetime.
Matt
That's incredibly wrong. It is highly likely that the UL is dynamic.
Justin Johnson
How are you determining the UL is "highly likely" to change size after page load?OP's code snippet is written as if children().length will in fact not change.
Matt
Only a small portion of the code is visible. Without an explanation of intent from the OP, your assumption is illegitimate.
Justin Johnson
Perhaps, but the available code certainly does not support a "high probability" of the UL changing at runtime.
Matt
The code doesn't make much of an indication either way. You are correct in that it could be inferred that the values *may* never change, or that whether or not the change *may* be irrelevant to this portion of the code; however, being that the OP is having problems understanding scope and using strange terminology like "loose javascript," it is highly likely that s/he may be taking the wrong approach altogether. Neither of our assumptions are verifiable until the OP makes some sort of a statement one way or the other.
Justin Johnson
A: 

In JavaScript (not ECMAScript) you can use this:

testimonialsBelt={
        parentElment:#1=$(".testimonialsCntnr ul"),
        childrenElem:#1#.children().length
}
Eli Grey
Does this work in IE?
Justin Johnson
@Justin: not even close.
Crescent Fresh