Badger
Badger

Reputation: 25

Why does my Javascript object outputs its method as typeof function instead of its result?

Why does Javascript return the baz method as 'typeof function' instead of its result and how can I output the result instead?

    var someObject  = new Object();
    someObject.foo  = $('#someElement'),
    someObject.bar  = $('#someOtherElement'),
    someObject.baz  = function() {
        return 'Hello world!';
    }

    console.log(someObject.baz); // Outputs: function (){return"Hello world!"}

Eventually I want to let baz calculate the height difference of the elements in foo and bar but so far I haven't been able to access the calculated result in baz or any other result like the string in the code example above.

Upvotes: 1

Views: 67

Answers (2)

Kiran Shinde
Kiran Shinde

Reputation: 5992

call the function

console.log(someObject.baz())

Upvotes: 0

Voonic
Voonic

Reputation: 4785

Because it is holding a reference to the function. If you want to return the result then you have to execute it

ex: someObject.baz()

Upvotes: 1

Related Questions