Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

「评论」JavaScript基础知识梳理(下) #56

Open
dongyuanxin opened this issue Jun 10, 2019 · 10 comments
Open

「评论」JavaScript基础知识梳理(下) #56

dongyuanxin opened this issue Jun 10, 2019 · 10 comments

Comments

@dongyuanxin
Copy link
Owner

dongyuanxin commented Jun 10, 2019

JavaScript基础知识梳理(下):https://xin-tan.com/passages/2019-03-27-javascript-second/

@SecondHandCode
Copy link

原型
题目:如何 JS 中的原型?
缺少了 '理解' 二字

@dongyuanxin
Copy link
Owner Author

@SecondHandCode
原型
题目:如何 JS 中的原型?
缺少了 '理解' 二字

已修改

@dadawanan
Copy link

执行上下文那好像说的不是很对

@dongyuanxin
Copy link
Owner Author

@dadawanan
执行上下文那好像说的不是很对

哪里不对?

@root-lucas
Copy link

root-lucas commented Nov 7, 2019

function mockData() {
 const mem = {name:"lucas",age:22};

  return {
    clear: ()=>{mem = null}, // 显式暴露清理接口

    get: page => {
      if (page in mem) {
        return mem[page];
      }
      mem[page] = Math.random();
    }
  };
}

console.log(mockData().get('name')); //lucas
mockData().clear();
console.log(mockData().get('name')); //lucas

执行到mockData().clear();直接报错Assignment to constant variable,即便换了let var赋值对象还是可以读取对象(依旧没有清除)

@dongyuanxin
Copy link
Owner Author

@root-lucas

function mockData() {
 const mem = {name:"lucas",age:22};

  return {
    clear: ()=>{mem = null}, // 显式暴露清理接口

    get: page => {
      if (page in mem) {
        return mem[page];
      }
      mem[page] = Math.random();
    }
  };
}

console.log(mockData().get('name')); //lucas
mockData().clear();
console.log(mockData().get('name')); //lucas

执行到mockData().clear();直接报错Assignment to constant variable,即便换了let var赋值对象还是可以读取对象(依旧没有清除)

没问题呀,const声明是地址不变的常量,重新赋值的操作会抛出 Assignment to constant variable

@root-lucas
Copy link

@dongyuanxin

@root-lucas

function mockData() {
 const mem = {name:"lucas",age:22};

  return {
    clear: ()=>{mem = null}, // 显式暴露清理接口

    get: page => {
      if (page in mem) {
        return mem[page];
      }
      mem[page] = Math.random();
    }
  };
}

console.log(mockData().get('name')); //lucas
mockData().clear();
console.log(mockData().get('name')); //lucas

执行到mockData().clear();直接报错Assignment to constant variable,即便换了let var赋值对象还是可以读取对象(依旧没有清除)

没问题呀,const声明是地址不变的常量,重新赋值的操作会抛出 Assignment to constant variable

@dongyuanxin

@root-lucas

function mockData() {
 const mem = {name:"lucas",age:22};

  return {
    clear: ()=>{mem = null}, // 显式暴露清理接口

    get: page => {
      if (page in mem) {
        return mem[page];
      }
      mem[page] = Math.random();
    }
  };
}

console.log(mockData().get('name')); //lucas
mockData().clear();
console.log(mockData().get('name')); //lucas

执行到mockData().clear();直接报错Assignment to constant variable,即便换了let var赋值对象还是可以读取对象(依旧没有清除)

没问题呀,const声明是地址不变的常量,重新赋值的操作会抛出 Assignment to constant variable

@root-lucas
Copy link

root-lucas commented Nov 10, 2019

清理闭包方法

function mockData() {
  const mem = {name:"lucas",age:22};

  return {
    clear: () => {
        for(let i in mem){
            delete mem[i];
        }
    }, // 显式暴露清理接口
    get: (page) => {
      if (page in mem) {
        return mem[page];
      }
      mem[page] = Math.random();
    }
  };
}

var result = mockData();

console.log(result.get('name')); //lucas
result.clear(); //清理常量对象
console.log(result.get('name')); //undefined

@root-lucas
Copy link

我觉得你这博客的模板不错,想知道在哪下载的?

@dongyuanxin
Copy link
Owner Author

@root-lucas 我觉得你这博客的模板不错,想知道在哪下载的?

vuepress官方模版,自行修改和定制插件即可

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants