欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

作用域面试题

程序员文章站 2022-04-18 22:55:17
...
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>04_作用域_面试题</title>
</head>
<body>
<script type="text/javascript">
  var x = 10;
  function fn() {
    console.log(x);
  }
  function show(f) {
    var x = 20;
    f();      //10
  }
  show(fn);


</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>04_作用域_面试题2</title>
</head>
<body>
<script type="text/javascript">
  var fn = function () {
    console.log(fn)   
  }
  fn()

  var obj = {
    fn2: function () {
     console.log(fn2)     //报错
     //console.log(this.fn2)
    }
  }
  obj.fn2()
</script>
</body>
</html>
相关标签: javascript