gpt4 book ai didi

javascript - CategorySelect 不是构造函数

转载 作者:行者123 更新时间:2023-11-30 16:10:19 25 4
gpt4 key购买 nike

我正在尝试让一个多选的 jsfiddle 工作,但我在控制台中收到一个错误:

CategorySelect is not a constructor

为什么我的 CategorySelect 不是构造函数?

代码:

var CategorySelect = (function() {
function CategorySelect() {
var _this = this;
this._configureMultiselect = function() {
_this._multiselectConfiguration = {
buttonWidth: '100%',
buttonContainer: '<div style="height: 64px;" />',
buttonClass: 'none',
nonSelectedText: "select category",
includeSelectAllOption: true,
disableIfEmpty: true,
onSelectAll: function() {
if (!$("#category-div span.multiselect-selected-text").text()) {
$("#category-div span.multiselect-selected-text").text('Select category');
}
},
onChange: function(option, checked) {
_this._selectedCategories = ko.observableArray([]);
var self = _this;
$('#category-select option:selected').each(function() {
self._selectedCategories.push(($(this).text(), $(this).val()));
});
console.log(_this._selectedCategories());
}
};
};
this._instantiateCategories = function() {
_this._categories = ko.observableArray([
new Category("Vegan Meat", 1),
new Category("Vegan Dairy", 2),
new Category("Confectionary", 3),
new Category("Baking", 4),
new Category("Dessert", 5),
new Category("Restaurants", 6),
new Category("Grocers", 7)
]);
};
this._configureMultiselect();
this._instantiateCategories();
$("#category-select").multiselect(this._multiselectConfiguration);
}
return CategorySelect;
})();

Here is the jsfiddle

最佳答案

声明是 hoisted到 JavaScript 范围的顶部,但绝不是它们的赋值。

当你引用 CategorySelect 时,它还没有被设置为任何东西,就像在包含范围的顶部有一个 var CategorySelect;,领先其值为 undefined,因此不是构造函数

jsFiddle .

关于javascript - CategorySelect 不是构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36351517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com