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?
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:Ideally though, you would make your objects through a constructor function, and the confirm method would return a new instance of
SomeClasswith thestatusflag set to true or false.