Reputation: 10771
Trying to get my head around javascript OOP, why is this causing the test method to print the entire function definition as if it was a string?
var Myclass = function Myclass(){
this.connection = make_ajax();
this.hasConnection = function(){return this.connection};
this.test = function(){
console.log(this.hasConnection);
}
}
var x = new Myclass();
x.test();
Result:
log: function(){return this.connection}
Upvotes: 0
Views: 62
Reputation: 1
When you
consloe.log(this.hasConnection);
you are logging the actual function, not the result of calling the function. Try changing this line to
consloe.log(this.hasConnection());
Upvotes: 0
Reputation: 324650
Because... you're not calling the function. You're logging the function itself, and since logging requires a string it calls the built-in toString
method which returns the function as a string.
Try console.log(this.hasConnection());
Upvotes: 3