Reputation: 43
I need that value of a variable such that i get alert "Yes", in the code.
var a;
// set the value of a here ...
a=?
// don't edit the code below:
if (a === a) {
alert('No!');
} else {
if (isNaN(a)) {
alert('Not again!');
}
else {
alert('Yes');
}
}
What is a?
Upvotes: 2
Views: 101
Reputation: 2172
First, thanking @skbhardwaj.india for asking the question and thanking @torazaburo for answering the question.
Inspired by @torazaburo's answer, I would like to be independent of external/global variable hence using closures.
Object.defineProperty(window, 'a' , { get: (function(p){ return function(){ return p++; }})(0) });
Upvotes: 0
Reputation:
Consider the following:
n = 0;
Object.defineProperty(window, 'a', { get: function() { return n++; } });
> a === a
< false
Upvotes: 2