0

I am fairly new to javascript and jquery. I thought i had learned enough to do this, but apparently not. I think the code is fairly self explanatory.

The problem is that ArrowDir is apparently undefined. It looks defined to me, but I'm sure I'm missing something obvious.

function ArrowDir(){
    var up = function(){
        $("#arrow").attr('src','up_arrow.jpg');
    };

    var down = function(){
        $("#arrow").attr('src','down_arrow.jpg');
    };
}

$(function () {
    if($("#toggle").onclick){
        ArrowDir().down();
    };
});

I've tried assigning the function as a variable, var ArrowDir = function(), but it doesn't seem to make a difference

4
  • I've updated the indentation and formatting to match the original post, and left the missing quote on purpose. Please update the code in your question if the missing quote was a transcription error. Commented Jun 19, 2013 at 16:01
  • Also, please review editing help if you haven't already. Commented Jun 19, 2013 at 16:02
  • Don't have a clue how that happened. I think i accidentally broke it up when pasting. It is fine in my code. It isn't the problem, either way. Thanks, anyway. Commented Jun 19, 2013 at 16:02
  • possible duplicate of Javascript: Do I need to put this.var for every variable in an object? Commented Jun 19, 2013 at 16:09

5 Answers 5

4

You can't access the up and down values in the manner that you've written. You'd need to simply call down() from within the ArrowDir body, unless you've added those functions to the ArrowDir return value:

ArrowDir() {
  var up = ...;
  var down = ...;
  return {
    up: up,
    down: down
  };
}

Alternatively, if you're not using ArrowDir for anything other than encapsulating the up and down functions, you should just declare ArrowDir as an object, and call ArrowDir.up() and ArrowDir.down():

var ArrowDir = {
    up: function () {
        ...
    },
    down: function () {
        ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming the missing quote after "#toggle is a typo, I'm not sure how you expect your code to work.

Here's how it runs, in prose:

  • Define a function ArrowDir.
  • When ready, attach a click handler
  • When clicked, call ArrowDir
    • In ArrowDir, define two local variables up and down, each with a function to do something.
    • There is no return statement, so return nothing
  • Call the down method of the "nothing" object. ERROR

See?

Try adding return {up:up,down:down}; to the end of your ArrowDir function.

Comments

0

You can create the object this way. In your function you are creating them insdied the scope of itself. SO it is not accessible outside.

function ArrowDir() {


}
ArrowDir.prototype.up = function () {
    $("#arrow").attr('src', 'up_arrow.jpg');
};

ArrowDir.prototype.down = function () {
    $("#arrow").attr('src', 'down_arrow.jpg');
}

and access it as

$(function () {
    if($("#toggle").onclick){
      var arrow =  new ArrowDir(); // call the constructor to create a new instance
      arrow.down(); // invoke the method
    };
});

4 Comments

Sorry, but no. This would only work if the function call were new ArrowDir().down().
It adds excessive complexity?
@Kolink Why. it just simple protoyping right. I did not say it has to be this way either.
@Kolink sure but no i dont agree regd more work, i am just adding to to the prototype. its just another way. Dont this this is worth a downvote.
0

ArrowDir().down() here's your problem. Just because you define a variable in the ArrowDir function doesn't make it a method/property of it. You have to use Prototype Inheritance.

EDIT:

//Something like this:
ArrowDir.prototype.down = function() {
    //Do stuff
}

Now you can call ArrowDir.down(). Doing this extends the properties (things it can do) of the object ArrowDir. You're adding a method to it.

3 Comments

"You have to use Prototype Inheritance" - Care to explain how?
I think this video can explain much better then I ever could: tutsplus.com/lesson/…
See this MSO answer.
0

The var up and down you have defined inside ArrowDir are not accessible outside the scope of that function.

The simplest way you could fix this, if this is a one-time kind of function, is just to put the up/down behavior in your jQuery function, like this:

$(function () {
    if($("#toggle").onclick){
        $("#arrow").attr('src','up_arrow.jpg');
    } else {
        $("#arrow").attr('src','down_arrow.jpg');
    }
});

If you just want to namespace these functions for reusability you could make an object literal instead:

ArrowDir = {
  up: function(){
    $("#arrow").attr('src','up_arrow.jpg');
  },
  down: function(){
    $("#arrow").attr('src','down_arrow.jpg');
  }
};

Then you can call: ArrowDir.up() or ArrowDir.down() elsewhere.

For what it's worth, if your goal is just to namespace these functions for reusability, I would say the object literal syntax makes more sense to me.

Or, if you really want to do it as a function call, as Kolink has pointed out, the up/down functions need to be in the return value. You could write that like this...

function ArrowDir() {
  var up = function(){
    $("#arrow").attr('src','up_arrow.jpg');
  }
  var down = function(){
    $("#arrow").attr('src','down_arrow.jpg');
  }
  return {up:up,down:down};
}

Or like this...

function ArrowDir() {
  return {
    up: function(){
      $("#arrow").attr('src','up_arrow.jpg');
    },
    down: function(){
      $("#arrow").attr('src','down_arrow.jpg');
    }
  };
}

Now calling ArrowDir().down() should work.

2 Comments

Kinda, since you're completely changing the structure of their code. What if ArrowDir is used elsewhere?
Ok @Kolink, much more discussion added with multiple different approaches including just fixing his return value...

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.