Lukas Oppermann
Lukas Oppermann

Reputation: 2938

JS call function when calling object without method

so I have an object Obj with some methods. If the object is called without any method I would like to run a specific method. Is that possible? If so how?

e.g.

function obj(variable){
    this.method = function(){};
    this.noMethod = function(){};
}
var test = new obj(variable);

Calling console.log(test) should now call noMethod on test. I hope you get what I mean.

The object has a nodelist if you call a method, it runs the method. If you just call the object it should return the nodelist and possible alter it, depending on the argument.

Is there maybe a way to check if the object is called with a method? This way I could have an if-Statement to check and run my method if no other method is called.

Upvotes: 2

Views: 918

Answers (5)

Alex KeySmith
Alex KeySmith

Reputation: 17101

From your comments on the question, this doesn't quite solve what you are after, I'm not sure if that's really possible, as I think your trying to execute a function without calling a function - which is sort of possible with constructor ideas - but again I don't think you're after that.

But this sort of structure might give you some ideas for alternatives (though admittedly it's not really what you are after).

var test = function() {
    alert('hello');

    return {
        moo : function() {
            alert('moo');
        }
    }
};

var obj = test(); //hello

obj.moo(); //moo

http://jsfiddle.net/6Vpfa/


EDIT:

Maybe it does help, happy days :-)

Upvotes: 0

mrpanda
mrpanda

Reputation: 104

have you looked into the arguments.length for javascript

function obj(variable, bool){
    if(bool)
     this.method = function(){}; 
    else
     this.noMethod = function(){};
}
var test = new obj(something, false);

calling console.log(test) should result in this.noMethod

Upvotes: 1

Oriol
Oriol

Reputation: 288290

Note that the exact behavior you want is not possible (afaik), because

  • You want to call test.noMethod without converting test into string, nor making it a function calling it. That means you need a getter.
  • But if you add a getter to test, then test.otherMethod will call test.noMethod too!

Some alternatives:

  • Use .toString() method:

    function Obj(variable){
        this.method = function(){};
        this.noMethod = function(){ alert('foo'); };
        this.toString = function(){ return this.noMethod(); }
    }
    var test = new Obj();
    test+''; // Calls test.noMethod, and returns test.noMethod()
    
  • Use getters (1):

    function Obj(variable){
        this.method = function(){};
        this.noMethod = function(){ alert('foo'); };
    }
    var obj = {test: new Obj()}, tmp = obj.test;
    Object.defineProperty(obj, 'test', {
      get: function(){ return tmp.noMethod() }
    });
    obj.test; // Calls tmp.noMethod, and returns test.noMethod()
    obj.test.noMethod(); // Calls test.noMethod once and throws an error!
    
  • Use getters (2):

    function Obj(variable){
        this.method = function(){};
        this.noMethod = function(){ alert('foo'); };
    }
    var obj = {test: new Obj()}, tmp = obj.test;
    Object.defineProperty(obj, 'test', {
      get: function(){ tmp.noMethod(); return tmp }
    });
    obj.test; // Calls tmp.noMethod, and returns tmp
    obj.test.noMethod(); // Calls test.noMethod twice!
    
  • Use function:

    function Obj(variable){
        var f = function(){ return f.noMethod(); };
        f.method = function(){};
        f.noMethod = function(){ alert('foo'); };
        return f;
    }
    var test = new Obj();
    test(); // Calls test.noMethod, and returns test.noMethod()
    test instanceof Obj // false!
    

Upvotes: 1

Mouseroot
Mouseroot

Reputation: 1064

This should call no method when used, and a method when called, if not then im not sure you can accomplish this without a function.

function Obj() {
  this.method = function(){};
  this.noMethod = function(){};
  return this.noMethod();
}

I originally said to use the arguments var but that still doesn't fit your request.

Upvotes: 0

Andrei B
Andrei B

Reputation: 2770

You are looking for this

function obj(){
    this.method = function(){};
    this.noMethod = function(){};
    this.toString = function() {return 'x'}
}
var test = new obj();
alert(test)

or this:

function obj(){
    this.method = function(){};
    this.noMethod = function(){};
}
obj.prototype.toString = function() {return 'x'}
var test = new obj();
alert(test)

See more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString

Upvotes: 0

Related Questions