js 面试的坑

js 面试的坑_第1张图片

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <script type="text/javascript"> (function(n){ for (var i = 0; i <n; i++) { setTimeout(function(){ console.log(i); },1) } })(6) </script>
</head>
<body>

</body>
</html>

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    <script type="text/javascript"> function add(n){ this.n=n; this.m=function(){ return this.n/2; } } var A=new add(20); alert(A.n+" "+A.m()); //20 10 A.n=40; alert(A.n+" "+A.m()); //40 20 </script>

你可能感兴趣的:(JavaScript,函数)