标签:++ 程序语言 alert speed 面试题 get 执行 tle 返回
<script> function outFun() { var num = 1; function inFun() { console.log(num); } } </script>
<script> function outFun() { var num = 1; function inFun() { console.log(num); } return inFun; } var demo = outFun(); demo(); </script>
<script> function outFun() { var a = 0; function inFun() { a ++; alert(a) } return inFun; } var obj = outFun(); obj(); obj(); var obj1 = outFun(); obj1(); obj1(); </script>
//1 2 1 2
闭包优点:不产生全局变量,实现属性私有化。
缺点:闭包的数据会常驻内存,在不用的时候要删掉,否则会导致内存溢出。
<script> function Fun(x) { return function (y) { console.log(x+y); } } var obj = Fun(2); obj(3); </script>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> div{ position: absolute; left: 0px; width: 200px; height: 200px; background-color: pink; } </style> </head> <body> <button id="btn2">左</button> <button id="btn1">右</button> <div id="box"></div> <script> var btn1 = document.getElementById("btn1"); var btn2 = document.getElementById("btn2"); var box = document.getElementById("box"); function move(speed) { return function () { box.style.left = box.offsetLeft + speed + "px"; } } btn1.onclick = move(5); btn2.onclick = move(-5); </script> </body> </html>
标签:++ 程序语言 alert speed 面试题 get 执行 tle 返回
原文地址:http://www.cnblogs.com/bky-1083/p/6363337.html