ES5 创建构造函数的私有属性

时间:2021-09-10 01:01:29   收藏:0   阅读:30

index.js

function Foo() {
  const list = [];
  this.push = function (val) {
    list.push(val);
  };
  this.getList = function () {
    console.log(list);
  };
}

const foo1 = new Foo();
const foo2 = new Foo();
foo1.push(1); 
foo1.getList(); // [ 1 ]
foo2.getList(); // []

原文:https://www.cnblogs.com/aisowe/p/15245929.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!