3

I'm new to javascript and I'm trying to figure out how to do something like this

SomeClass = {

    confirm: function(){
        if(!confirm('Sure?'))
        {
            return false;
        }
    },

    someMethod: function(){
        alert("OK");
    }

}

And this is what I actually want to do

SomeClass.confirm().someMethod();

In fact i need to confirm an action just by adding .confirm() in front of the actual method. Is that even possible?

1

5 Answers 5

5
SomeClass = {

    confirm: function(){
        if(!confirm('Sure?'))
        {
            this.status=false;
            return this;
        }
    },

    someMethod: function(){
        if(this.status){
            alert("OK");
        }
    }

}

This kind of method chaining is only possible if you return the object itself. You can use status to indicate whether the user confirmed or not.

You could also return just an object with the appropriate somemethod, although I suspect you want the method chaining to be more general than this:

SomeClass = {

    confirm: function(){
        if(!confirm('Sure?'))
        {
            return {someMethod:function(){alert("Canceled");}};
        }
        return {someMethod:function(){alert("OK");}};
    }

}

Ideally though, you would make your objects through a constructor function, and the confirm method would return a new instance of SomeClass with the status flag set to true or false.

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

Comments

3

To do a fluent interface, you need to return the object itself from each function call. That means restructuring your if logic:

SomeClass = {

wasConfirmed: false,

confirm: function(){
    this.wasConfirmed = confirm('Sure?');

    return this;
},

someMethod: function(){
    if(this.wasConfirmed) alert("OK");
}

}

2 Comments

Although you'll need some mechanism to clear the wasConfirmed flag too.
Correct. The OP didn't really indicate where/when that should happen though so I left it as part of the constructor only.
0

If I understand you correctly, you want any action to be "confirmable" then you could write a class which takes the message to display tyo the user, and the action to perform if it's "confirmed"

var Confirmer = {

  confirm: function(msg, action){
       if(confirm(msg)){
          action();
       }
  }

}

You would then call it like:

Confirmer.confirm("Are you sure?", function(){ alert("hello"); });

With the second parameter any function you want called after the user confirms.

Comments

0

You will not be able to chain method calls in the way you described:

SomeClass.confirm().someMethod();

if your confirm method returns anything other than the SomeClass object that has the someMethod() defined.

You have probably seen things like this in jQuery:

// trigger click handlers of the element with id="id" and hide it:
$("#id").click().hide();

It works because the click() method returns the same object on which it was itself invoked on, so that the hide() method could be called on that returned object. Methods that return anything other than a jQuery object cannot be chained even in jQuery (only as the last method in chain).

Comments

0

Try

var SomeClass = {
  state: null,

  confirm: function(){
    this.state = confirm('Sure?');    

    return this;
  },

  someMethod: function() {
    alert(this.state ? 'OK' : 'Error');
  }
};

SomeClass.confirm().someMethod();

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.