1、 阅读代码

  1. (function(){
  2. var x = y = 1;
  3. })();
  4. var z;
  5. console.log(y);
  6. console.log(z);
  7. console.log(x);

2、 阅读代码

  1. var a, b
  2. (function () {
  3. console.log(a);
  4. console.log(b);
  5. var a = (b = 3);
  6. console.log(a);
  7. console.log(b);
  8. })()
  9. console.log(a);
  10. console.log(b);

3、 阅读代码

  1. var friendName = 'World';
  2. (function() {
  3. if (typeof friendName === 'undefined') {
  4. var friendName = 'Jack';
  5. console.log('Goodbye ' + friendName);
  6. } else {
  7. console.log('Hello ' + friendName);
  8. }
  9. })();

4、 阅读代码

  1. function fn1(){
  2. console.log('fn1')
  3. }
  4. var fn2
  5. fn1()
  6. fn2()
  7. fn2 = function() {
  8. console.log('fn2')
  9. }
  10. fn2()

5、 阅读代码

  1. function a() {
  2. var temp = 10;
  3. function b() {
  4. console.log(temp); // 10
  5. }
  6. b();
  7. }
  8. a();
  9. function a() {
  10. var temp = 10;
  11. b();
  12. }
  13. function b() {
  14. console.log(temp); // 报错 Uncaught ReferenceError: temp is not defined
  15. }
  16. a();

6、 阅读代码

  1. var a=3;
  2. function c(){
  3. alert(a);
  4. }
  5. (function(){
  6. var a=4;
  7. c();
  8. })();

7、 阅读代码

  1. function fun(n, o) {
  2. console.log(o)
  3. return {
  4. fun: function(m){
  5. return fun(m, n);
  6. }
  7. };
  8. }
  9. var a = fun(0); a.fun(1); a.fun(2); a.fun(3);
  10. var b = fun(0).fun(1).fun(2).fun(3);
  11. var c = fun(0).fun(1); c.fun(2); c.fun(3);

8、 阅读代码

  1. f = function() {return true;};
  2. g = function() {return false;};
  3. (function() {
  4. if (g() && [] == ![]) {
  5. f = function f() {return false;};
  6. function g() {return true;}
  7. }
  8. })();
  9. console.log(f());