闭包

                 /*立即执行函数*/

//*例题2
//[01:31 --> 09:49 ] 闭包 [28:50 --> 30:31]
// function a() {
// function b() {
// var bbb = 234;
// console.log(aaa);
// }
// var aaa = 123;
// return b;
// }

// var golb = 100;
// var demo = a();
// demo();

//例题3
//[10:00 --> 21:10] ]

// function a() {

// var num = 100;
// function b() {
// num++;
// console.log(num)
// }
// return b;

// }
// var demo = a();
// demo();
// demo();

// 闭包:当内部函数被保存到外部时,将会生成闭包。闭包会导致原有作用域链不释放,造成内存泄露。

// 闭包的作用:实现公有变量
// 函数累加器
// [35:31 --> 36:25]
// function add() {
// var count = 0;
// function demo() {
// count++;
// console.log(count)
// }
// return demo;
// }

// var counter = add();
// counter();
// counter();
// counter();
// counter();
// counter();
// counter();

// 可以做缓存(存储结构)
// eater
// 例题1:
// [39:25 --> 42:00 -->48:00 ]
// function test() {
// var num = 100;
// function a() {
// num ++;
// console.log(num)
// }
// function b() {
// num–;
// console.log(num)
// }
// return [a,b]
// }

// var myArr = test();
// myArr0;
// myArr1;

// 例题2
// [48:40 --> 52:27]
// function eater() {
// var food = “”;
// var obj = {
// eat : function () {
// console.log('i am ’ + food);
// food = “”;
// },
// push : function (myFood) {
// food = myFood;
// }
// }
// return obj;
// }

// var eater1 = eater();

// eater1.push(‘banana’);
// eater1.eat();

// 可以实现封装,属性私有化
// Person()

// 模块化开发,防止污染全局变量

            /*闭包*/

//例题1
//[01:33 --> 16:28 --> 26:07]
//一个for循环,数组定义为10,给数组的每一位都变成函数,函数的作用就是打印当前每一位的索引
// function test() {
// var arr = [];
// for (i = 0; i < 10; i ++){
// arr[i] = function () { //把一个函数体付给数组的当前位,数组的当前位是要被马上索取出来的
// document.write(i + " ");//函数体不是现在执行(函数引用,系统现在不知道,只有在执行的时候系统才会读里面的语句)
// } //*它没有在当前执行
// }
// return arr;
// }
// var myArr = test();
// for (var j = 0; j < 10; j++) {
// myArrj; //*它是在这执行的,所以它就索取10
// }

//例题1的解决方法
//[29:49 --> 36:01 -->结束]
// function test() {
// var arr = [];
// for (i = 0; i < 10; i ++){
// (function (j) {
// arr[j] = function () {
// document.write(j + " ");
// }
// }(i));
// }
// return arr;
// }
// var myArr = test();
// for (var j = 0; j < 10; j++) {
// myArrj;
// }

                        /*闭包精细版*/

//例题1:[04:00 --> 05:22]
// function test() {
// var tem = 100;
// function a() {
// console.log(tem);
// }
// return a;
// }

// var demo = test();
// demo();

//例题2 [17:52 --> 19:11]
//一个不用return的闭包函数
// var demo;
// function test() {
// var abc = 100;
// function a() {
// console.log(abc);
// }
// demo = a;
// }
// test();
// demo();

// 闭包:当内部函数被保存到外部时,将会生成闭包。闭包会导致原有作用域链不释放,造成内存泄露。
// [20:05 --> 23:00]

//例题3:实现函数累加器的作用
//[23:42 --> 31:20]
//不利用闭包的情况
// var num = 0;
// function a() {
// num++;
// console.log(num);
// }
// a();

//利用闭包的情况下
// function add() {
// var num = 0;
// function a() {
// console.log(++ num);
// }
// return a;
// }
// var myAdd = add();
// myAdd();

//例题4:做缓存
//[33:03 --> ]
// function test() {
// var food = “apple”;
// function a() {
// console.log(food);
// }
// function b() {
// console.log(food);
// }
// }

//例题5[37:55 --> 42:10]
// function test() {
// var food = “apple”
// var obj = {
// eatFood : function () {
// if (food != “”) {
// console.log("I am eating " + food);
// food = “”;
// }else{
// console.log(“是空的”);
// }
// },
// pushFood : function (myFood) {
// food = myFood;
// }
// }
// return obj;
// }

// var Person = test();

// Person.eatFood();
// Person.eatFood();
// Person.pushFood(‘banana’)
// Person.eatFood();

//例题6:在一个函数中添加一个数组,给数组添加10个值,这十个值都是函数,每个函数分别打印1-10
// [47:25 --> 52:06]
// function test() {
// arr = [];
// for (i = 0; i < 10; i++) {
// (function (j) {
// arr[j] = function () {
// console.log(j)
// }
// }(i))
// }
// return arr;
// }

// var myArr = test();
// for (var j = 0; j < 10; j++) {
// myArrj;

// }

//例题7:[1:45:35 --> 1:50:50]
// a = 100;
// function demo(e) {
// function e() {}
// arguments[0] = 2;
// document.write(e);
// if (a) {
// var b = 123;
// function c() {
// //嘤嘤嘤
// }
// }
// var c;
// a = 10;
// var a;
// document.write(b);
// f = 123;
// document.write©;
// document.write(a);
// }
// var a;
// demo(1);
// document.write(a);
// document.write(f);

//例题8:写一个方法,求一个字符串的字节长度(提示:字符串有一个方法charCodeAt();一个中文占2个字节,一个英文占一个字节);
//[1:51:50 --> 2:00:56]
// function retByte(target) {

// count = 0;
// for (var i = 0; i < target.length; i++) {
// if (target.charCodeAt(i) <= 255) {
// count ++;
// }else if (target.charCodeAt(i) > 255) {
// count += 2;
// }
// }
// console.log(count);
// }

//将上题优化
// function retByte(target) {
// var count,
// len,
// count = len = target.length;
// for (var i = 0; i < len; i++) {
// if (target.charCodeAt(i) > 255) {
// count ++;
// }
// }
// console.log(count);
// }

//例题9 [2:03:56 --> 2:07:39]
// var f = (
// function f() {

// return “1”;

// },
// function g() {

// return 2;

// }
// )();
// typeof f;

//例题10 [2:11:23 --> 2:18:46]
// var x = 1;
// if (function f() {}) {
// x += typeof f;
// }
// console.log(x);

</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值