var editor = {
"test1": document.getElementById("a"),
"test2": function() {
return document.getElementById("a")
},
"editCont": function() {
console.log(editor.test1.id); // this returns 'cannot read property 'id' of null'
console.log(editor.test2().id); // when removing the above console.log, this returns 'a'
}
}
<h1 id="a" onclick="editor.editCont()">Click me!</h1>
the console returns null for test1 and 'a' for the second.
Can editor.test1 work without having to put it in a function like editor.test2?
I understand that DOM element cannot be found if it doesn't exist however the second console.log works, whats the reason for the first console.log?