What does this point to in this fiddle?
This copies the core parts of jQuery into a simple fiddle.
How can I test for what this points to?
Reference
What does this point to in this fiddle?
This copies the core parts of jQuery into a simple fiddle.
How can I test for what this points to?
Reference
var $A = function (test) {
return new $A.prototype.init(test);
};
$A.prototype = {
init: function (test) {
var a = 'function_var';
this[0] = a;
this[1] = arguments[0];
}
};
document.getElementById('foo').innerHTML = $A('hi_there')[0];
console.debug(jQuery('hi_there'));
this
in "this" case points to the instance of the init
function.
Calling $A
's constructor function returns a new
instance of $A
's prototype init function.
Read this introduction into the this
keyword. You don't really know what this
points to, it depends on the invocation of the function.
However, as the init
function is invoked with the new
keyword, this
will be a new object that inherits from the init.prototype
object - an instance of the init constructor function.
To inspect the value of this
, use your debugger. You can also console.log(this)
.