Javascript 如何使用闭包保存变量状态
我有一个需要多次调用的函数,该函数将在我的html中附加元素:Javascript 如何使用闭包保存变量状态,javascript,node.js,closures,Javascript,Node.js,Closures,我有一个需要多次调用的函数,该函数将在我的html中附加元素: class MyClass { somefunct(elements) { const father = $("#father").find("#other-element"); for (let element of elements) { father.append(element); } } } 我希望避免在每次通话中都对父亲进行初始化。怎么会这样 我做得怎么样: somef
class MyClass {
somefunct(elements) {
const father = $("#father").find("#other-element");
for (let element of elements) {
father.append(element);
}
}
}
我希望避免在每次通话中都对父亲进行初始化。怎么会这样
我做得怎么样:
somefunct(elements) {
const father = $("#father").find("#other-element");
this.somefunc = (elements) => {
for (let element of elements) {
father.append(element);
}
}
}
这会起作用,但我不知道这是一种不好的做法,还是有更好的方法
谢谢。最好的方法是将父类声明为类的属性,并在构造函数中提取一次,如下所示:
class MyClass {
constructor() {
this._father = $("#father").find("#other-element");
}
somefunct(elements) {
for (let element of elements) {
this._father.append(element);
}
}
}
class MyClass {
// ... other methods
}
MyClass.prototype.somefunct = (function() {
const father = $("#father").find("#other-element");
return function(elements) {
for (let element of elements) {
father.append(element);
}
}
}());
class MyClass {
constructor(options){
this.$father = options.el;
}
somefunct(elements) {
for (let element of elements) {
this.$father.append(element);
}
}
}
let classInstance = new MyClass({
el: $("#father").find("#other-element")
});
classInstance.somefunct(elements);
但是在这种情况下,\u父成员将是puplic。如果要将其隐藏在闭包中,则在定义类方法时必须使用IIFE(立即调用的函数表达式),但由于ES类文字不允许IIFE,因此必须使用如下旧原型:
class MyClass {
constructor() {
this._father = $("#father").find("#other-element");
}
somefunct(elements) {
for (let element of elements) {
this._father.append(element);
}
}
}
class MyClass {
// ... other methods
}
MyClass.prototype.somefunct = (function() {
const father = $("#father").find("#other-element");
return function(elements) {
for (let element of elements) {
father.append(element);
}
}
}());
class MyClass {
constructor(options){
this.$father = options.el;
}
somefunct(elements) {
for (let element of elements) {
this.$father.append(element);
}
}
}
let classInstance = new MyClass({
el: $("#father").find("#other-element")
});
classInstance.somefunct(elements);
如果您使用ES6类。可以这样做:
class MyClass {
constructor() {
this._father = $("#father").find("#other-element");
}
somefunct(elements) {
for (let element of elements) {
this._father.append(element);
}
}
}
class MyClass {
// ... other methods
}
MyClass.prototype.somefunct = (function() {
const father = $("#father").find("#other-element");
return function(elements) {
for (let element of elements) {
father.append(element);
}
}
}());
class MyClass {
constructor(options){
this.$father = options.el;
}
somefunct(elements) {
for (let element of elements) {
this.$father.append(element);
}
}
}
let classInstance = new MyClass({
el: $("#father").find("#other-element")
});
classInstance.somefunct(elements);
第二个代码段更糟糕!您仍然在每次调用时获取父函数,并定义一个实际上没有任何改进的函数!我可以像MyClass.prototype={somefunc(){/*closure*/}}那样使用吗?然后你必须提供构造函数和其他方法,这样你就不需要ES的类文本语法,因为你必须重新定义这个类!我想!