
Source: WebDesignDegreeCenter.org
if (!("a" in window)) {
var a = 1;
}
alert(a);
window
. Writing var a = 1
is functionally equivalent to writing window.a = 1
. You can check to see if a global variable is declared, therefore, by using the following:"variable-name" in window
alert("a" in window);
var a;
var a;
alert("a" in window);
var a = 1;
var a; //declaration
a = 1; //initialization
var a;
if (!("a" in window)) {
a = 1;
}
alert(a);
a
is declared first, and then the if
statement says, “if a
isn’t declared, then initialize a
to have a value of 1.” Of course, this condition can never be true and so the variable a remains with its default value,undefined
.var a = 1,
b = function a(x) {
x && a(--x);
};
alert(a);
function functionName(arg1, arg2){
//function body
}
var functionName = function(arg1, arg2){
//function body
};
1
2
3
| 1. ( function (){ return typeof arguments; })(); |
1
2
| 2. var f = function g(){ return 23; }; typeof g(); |
1
2
3
4
| 3. ( function (x){ delete x; return x; })(1); |
1
2
| 4. var y = 1, x = y = typeof x; x; |
1
2
3
| 5. ( function f(f){ return typeof f(); })( function (){ return 1; }); |
1
2
3
4
5
6
7
| 6. var foo = { bar: function () { return this .baz; }, baz: 1 }; ( function (){ return typeof arguments[0](); })(foo.bar); |
1
2
3
4
5
6
7
| var foo = { bar: function () { return this .baz; }, baz: 1 }; ( function (){ return typeof arguments[0].call(foo); })(foo.bar); |
1
2
3
4
5
| 7. var foo = { bar: function (){ return this .baz; }, baz: 1 } typeof (f = foo.bar)(); |