get set function closure
by ian_smithz
HTML
<button class="btn">
Good morning
</button>
JavaScript
myFunction = function() {
lastFocus = document.activeElement;
return function(setTo) {
if (typeof(setTo) != "undefined") {
lastFocus = setTo;
return this;
} else {
return lastFocus;
}
}
}
debugger;
A = myFunction(); // myFunction returns a function, not a value
console.log(A()); // 'document.activeElement'
$('.btn').focus();
console.log(lastFocus); // 'document.activeElement'
//A('123'); // reassign variable
//console.log(A()); // '123'
//A(false); //works with 0 or false, too
//console.log(A()); // false