0

I am trying to access properties of a config object from within another object:

var myConfigObj = {
  $myCachedSelector: $('#mySelector')
}

var myObj = {
  $selector: myConfigObj.$myCachedSelector,
  url: 'http://www.someurl.com/somepath'
}

$.each([ myObj, mySecondObj, myThirdObj ], function() {
  this.$selector.load(this.url, function(){
  //do something
}); 

When trying to use $selector in the each function then, it returns "undefined". Both objects are in the same scope, and I don't know what the problem is.

1
  • Please add more code: what is myConfigObj.$myCachedSelector (maybe it's undefined)? And how are you using myObj.$selector? Commented Nov 4, 2009 at 14:15

2 Answers 2

1

This code worked well enough for me, with a few minor tweaks:

  • This is probably the problem: Firefox is complaining about the semicolon, should be:

    var myConfigObj = {
       $myCachedSelector: $('#mySelector')
    }
    
  • Should be in $(document).ready, of course.
  • Missing some }); at the end (probably a copy/paste thing)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Kobi, thanks for your reply. These were all c&p mistakes :) But the code is still not working for me. I get this error message in IE: "'this.$selector' is Null or not an object" and still "this.$selector is undefined" in FF...
Kobi, I found the cause of the errors: there was one wrong property name hidden in my code. I also had another function inside each and so 'this' didn't point to the dom object anymore. 'This' can sometimes drive you a little crazy... :)
1
  1. Install Firebug in Firefox;
  2. In the "Scripts" pane, set a breakpoint on the line that causes the error;
  3. When stopped at the breakpoint, examine this in the right-hand "Watch" pane;
  4. If the this object you're stopped on has a $selector property which has a load method then it's not the cause of your problem, so continue round the loop.
  5. When the this object doesn't have a $selector property, or has one that doesn't have a load method, you've found your culprit. Now work out why you are sending it into that loop, or where you are failing to initialise it.

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.