for (let i = 0; i < 10; i++) { result[i] = (function (num) { returnfunction () { return num; }; })(i); } return result; }
CG 回收(逃
7.2.2 关于 this 对象
1 2 3 4 5 6 7 8 9 10 11
let name = "The Window"; let object = { name: "My Object", getName: function () { let that = this; returnfunction () { return that.name; }; }, }; console.log(object.getName()());
可以使用 that 来保存 this
7.2.3 内存泄漏
略
7.3 模仿块级作用域
1 2 3
(function () { //这里是块级作用域 })();
7.4 私有变量
1 2 3 4 5 6 7 8 9 10
functionMyObject() { let privateVariable = 10; functionprivateFunction() { returnfalse; } this.publicMethod = function () { privateVariable++; return privateFunction(); }; }
1 2 3 4 5 6 7 8 9 10 11 12 13
functionPerson(name) { this.getName = function () { return name; }; this.setName = function (value) { name = value; }; }
var person = new Person("Nicholas"); console.log(person.getName()); person.setName("Greb"); console.log(person.getName());