Giter Site home page Giter Site logo

Comments (10)

SecondHandCode avatar SecondHandCode commented on July 21, 2024

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

from blog.

dongyuanxin avatar dongyuanxin commented on July 21, 2024

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

已修改

from blog.

dadawanan avatar dadawanan commented on July 21, 2024

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

from blog.

dongyuanxin avatar dongyuanxin commented on July 21, 2024

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

哪里不对?

from blog.

root-lucas avatar root-lucas commented on July 21, 2024
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赋值对象还是可以读取对象(依旧没有清除)

from blog.

dongyuanxin avatar dongyuanxin commented on July 21, 2024

@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

from blog.

root-lucas avatar root-lucas commented on July 21, 2024

@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

from blog.

root-lucas avatar root-lucas commented on July 21, 2024

清理闭包方法

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

from blog.

root-lucas avatar root-lucas commented on July 21, 2024

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

from blog.

dongyuanxin avatar dongyuanxin commented on July 21, 2024

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

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

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.