Trying to understand this
, found an example in javascript the good parts
book:
var first;
var second;
var Quo = function(string) {
first = this;
this.status = string;// first this
};
Quo.prototype.get_status = function() {
second = this;
return this.status;//second this
};
var myQuo = new Quo( "confused" );
console.log( myQuo.get_status() );
console.log( (first===second) + ',' + (second===myQuo) + ',' + (first===myQuo) );
Output:
$ node test.js
confused
true,true,true
Does the first this
and the second this
both point to myQuo
? How to print out the object name or function name or class name which each this
point to? (Really confused by this
currently.)
UPDATE
Another questions: Both this
refer to the instance of Quo instead of Quo's prototype?
Also, trying:
console.log( myQuo.get_status() );
console.log(first.constructor.name);
console.log( first );
console.log( second );
Output:
confused
{ status: 'confused' }
{ status: 'confused' }
Why first.constructor.name
is nothing? Why first
is { status: 'confused' }
?