Bobby
Bobby

Reputation: 13

Calling an object as a function as well as calling a function inside of that object

I'd like someone to confirm for me that it's impossible to create an object that allows me to make both of the following calls in javascript:

user.remove();
user.remove.all();

Upvotes: 1

Views: 45

Answers (2)

Anduril
Anduril

Reputation: 1276

No it's not impossible. functions are objects just like everything else. There is nothing to stop you doing the following:

function User() {
    var remover = function (){
        // do removal stuff 
    }

    remover.all = function () {
        // do remove all stuff
    } 

    this.remover = remover;
}

Then just create the user as normal:

var user = new User();

Upvotes: 0

jsxsl
jsxsl

Reputation: 200

Sure you can. Here is an example.

            var user = new Object();
            user.remove = function () {
                console.log("remove called");
            }
            user.remove.all = function (){
                console.log("remove all");
            }

            user.remove();
            user.remove.all();

Upvotes: 4

Related Questions