0
box_tpv1 = {
    box:$("#box_tpv1"),
    open:function(mensaje,f_ok,f_x){
        this.box.show()
    }
}

And when I call this box_tpv1.open() won't work, but If I write inside open function $("#box_tpv1").show() it works.

2
  • 4
    You have to use closures. this in your example refers to the anonymous function. Commented Jan 31, 2013 at 9:16
  • Try to add a console.log(this.box); in open function to see what it contains. Commented Jan 31, 2013 at 9:16

3 Answers 3

1

In your case, box_tpv1 is a singleton object, which cannot be further instantiated using new. Which means the value of this is insignificant.

You might as well simply call box_tpv1.box.show() inside the open function.

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

2 Comments

It won't work, and how It would be instatiated with new? I don't understand very well objects in javascript :(
This should clarify things regarding objects in JS: developer.mozilla.org/en-US/docs/JavaScript/Guide/…
0

there might be issues on the context this function is being called and that depends upon from where are you calling this function from try calling like this

box_tpv1.open.call(box_tpv1);

Comments

0

I don't know why but I solved it this way, I can get with this.box the value inside the object methods but doesnt work the jquery selector, if I do that it works

box_tpv1 = {
    box:"#box_tpv1",
    open:function(mensaje,f_ok,f_x){
        $(this.box).show()
    }
}

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.