0

I have an object from which I call a function. Instead of the value it returns the function itself. This might be a duplicate but I could not find a proper solution. So any buzzword for this matter would be highly appreciated.

var w = window,
    d = document,
    e = d.documentElement,
    g = d.getElementsByTagName('body')[0];

var asd = {
    getWindowWidth: function() {
        var x = w.innerWidth || e.clientWidth || g.clientWidth;
        return x;
    },
    getWindowHeight: function() {
        var x = (function() {
            return w.innerWidth || e.clientWidth || g.clientWidth;
        })();
        return x;
    },
    init: function() {
        console.log("init fired");
        console.log(this.getWindowWidth);
        console.log(this.getWindowHeight);
        console.log(typeof(this.getWindowHeight));
    }
}

asd.init();

Thank you in advance for your support.

2
  • Well console.log(this.getWindowWidth); returns the function console.log(this.getWindowWidth()); evaluates it and returns the result if thats what you asking (which is not very clear). Commented May 30, 2017 at 10:03
  • 1
    "I have an object from which I call a function" — No, you don't. You have an object from which you console.log a function. You have to put () after a pointer to a function to actually call it. Commented May 30, 2017 at 10:04

4 Answers 4

3

Use parenthesis to call the function, otherwise you have simply captured the function itself.

console.log(this.getWindowWidth());
//                             ^^
Sign up to request clarification or add additional context in comments.

Comments

2

Simply change your function like this, this.getWindowWidth()

without paranthesis it will do actual function call and it wont return a value.

var w = window,
    d = document,
    e = d.documentElement,
    g = d.getElementsByTagName('body')[0];

var asd = {
    getWindowWidth: function() {
        var x = w.innerWidth || e.clientWidth || g.clientWidth;
        return x;
    },
    getWindowHeight: function() {
        var x = (function() {
            return w.innerWidth || e.clientWidth || g.clientWidth;
        })();
        return x;
    },
    init: function() {
        console.log("init fired");
        console.log(this.getWindowWidth());
        console.log(this.getWindowHeight());
        console.log(typeof(this.getWindowHeight()));
    }
}

asd.init();

Comments

0

You are using this.getWindowHeight instead of this.getWindowHeight()

var w = window,
    d = document,
    e = d.documentElement,
    g = d.getElementsByTagName('body')[0];

var asd = {
    getWindowWidth: function() {
        var x = w.innerWidth || e.clientWidth || g.clientWidth;
        return x;
    },
    getWindowHeight: function() {
        var x = (function() {
            return w.innerWidth || e.clientWidth || g.clientWidth;
        })();
        return x;
    },
    init: function() {
        console.log("init fired");
        console.log(this.getWindowWidth());
        console.log(this.getWindowHeight());
        console.log(typeof(this.getWindowHeight()));
    }
}

asd.init();

Comments

0

the init function is invoked, but the other functions not so much. As Alex K. said you need to change

console.log(this.getWindowWidth);
console.log(this.getWindowHeight);
console.log(typeof(this.getWindowHeight));

to:

console.log(this.getWindowWidth());
console.log(this.getWindowHeight());
console.log(typeof(this.getWindowHeight()));

var w = window,
    d = document,
    e = d.documentElement,
    g = d.getElementsByTagName('body')[0];

var asd = {
    getWindowWidth: function() {
        var x = w.innerWidth || e.clientWidth || g.clientWidth;
        return x;
    },
    getWindowHeight: function() {
        var x = (function() {
            return w.innerWidth || e.clientWidth || g.clientWidth;
        })();
        return x;
    },
    init: function() {
        console.log("init fired");
        console.log(this.getWindowWidth());
        console.log(this.getWindowHeight());
        console.log(typeof(this.getWindowHeight()));
    }
}

asd.init();

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.