I thought I understood how javascript scope worked until now. This is what I have
function MFCommentsHandler(node, type, item) {
this.get = function(returnType){
...
}
function getButton(){
var button = document.createElement('button'),
get = this.get;
button.innerHTML = 'Load more comments';
button.onclick = function(){
console.log(get);
get('html');
}
return button;
}
}
I use inner functions to append the element generated by getButton to the document, but when I click it I get get is not a function. Why is get undefined inside of the onclick function?
thisis whatever context you're callingMFCommentsHandlerin. Unless you're constructing a new object usingnew.