[英]Call object recursive function in javascript
我是Java語言的新手。 我有一個JavaScript類,可以在內部調用遞歸。 但是運行時出現錯誤:
$(...)。display不是$(item).display(false);的函數。
這是我的代碼:
function ScopeData(table, parentRow) {
const tableData = table;
const scopeEl = parentRow;
var childScope = [];
var childRows = [];
this.display = function aa(isDisplay) {
childRows.each(function (index, el) {
if (isDisplay) {
$(el).removeClass("hidden");
}
else {
$(el).addClass("hidden");
}
});
if (!isDisplay) {
var display1 = this.display;
for (let i = 0; i < childScope.length; i++) {
const item = $(childScope)[i];
$(item).display(false);
}
}
}
this.findChildRow = function () {
var scope = $(scopeEl).data("scope")
childRows = $(tableData).find("tr[data-parent='" + scope + "']");
}
this.createChildScope = function () {
$(childRows).each(function (index, el) {
var scope = $(el).data("scope")
if (scope !== "" && scope !== undefined) {
var childRows = $(tableData).find("tr[data-parent='" + scope + "']");
var dataRow = new ScopeData(table, $(el));
dataRow.init();
childScope.push(dataRow);
}
});
}
this.init = function () {
var display = this.display;
this.findChildRow();
this.createChildScope();
$(scopeEl).find("i").on("click", function () {
display(false);
})
}
}
請幫助我解決問題。 預先感謝
歡迎使用JavaScript以及如何評估this
關鍵字。 我強烈建議你閱讀這繼續,因為它是Javascript的核心概念之一了。 解決問題的一種方法是:
this
分配到變量中,然后使用它。
function ScopeData(table, parentRow) {
var self = this;
...
this.init = function () {
var display = self.display;
一種更清潔的方法是綁定任何現代瀏覽器支持的功能。
this.display = function aa(isDisplay) {
childRows.each(function (index, el) {
if (isDisplay) {
$(el).removeClass("hidden");
}
else {
$(el).addClass("hidden");
}
});
if (!isDisplay) {
var display1 = this.display;
for (let i = 0; i < childScope.length; i++) {
const item = $(childScope)[i];
$(item).display(false);
}
}
}
this.display = this.display.bind(this);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.