Skip to content

Instantly share code, notes, and snippets.

@elrrrrrrr
Last active August 29, 2015 14:05
Show Gist options
  • Save elrrrrrrr/f411471f8c674f793eb3 to your computer and use it in GitHub Desktop.
Save elrrrrrrr/f411471f8c674f793eb3 to your computer and use it in GitHub Desktop.
javascript
var v='Hello World';
(function(){
alert(v);
var v='I love you';
})()
var v='Hello World';
(function(){
var v;
alert(v);
v='I love you';
})()
//结果是undefined
//scope
var x = 1;
console.log(x); // 1
if (true) {
var x = 2;
console.log(x); //2
}
console.log(x);// 2
//建立临时scope
function foo() {
var x = 1;
if (x) {
(function () {
var x = 2;
// some other code
}());
}
// x is still 1.
}
// 不同浏览器编译差异
if ( condition ) {
function A () {
}
}
console.log(A)
//在火狐下不会预编译
//log = console.log 无效
log = console.log.bind(console)
function say667() {
// Local variable that ends up within closure
var num = 666;
var sayAlert = function() { alert(num); }
num++;
return sayAlert;
}
var sayAlert = say667();
sayAlert()//执行结果应该弹出的667
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment