Javascript Can';t在闭包中运行函数
我刚开始想办法解决这个问题。具有私有变量和私有方法的。我遇到的问题是,当调用这些方法时,开发人员控制台告诉我“未定义的不是函数” Javascript:Javascript Can';t在闭包中运行函数,javascript,methods,closures,Javascript,Methods,Closures,我刚开始想办法解决这个问题。具有私有变量和私有方法的。我遇到的问题是,当调用这些方法时,开发人员控制台告诉我“未定义的不是函数” Javascript: var shop = (function() { var items = []; return { Item: function(data) { this.id = data[0]; this.item = data[1]; this.des
var shop = (function() {
var items = [];
return {
Item: function(data) {
this.id = data[0];
this.item = data[1];
this.description = data[2];
this.price = data[3];
this.count = data[4];
},
addItem: function(id, item, description, price) {
items.push(this.Item(id, item, description, price));
},
print: function() {
var itemCon = document.getElementById('items'),
html = "";
items.forEach(function(item) {
html += '<div class="item">';
for(prop in item) {
if(prop != "id") {
if(prop != "count") {
if(prop === "price") {
html += '<p><span class="title">' + prop + '</span>: £' + item[prop] + '</p>';
} else {
html += '<p><span class="title">' + prop + '</span>: ' + item[prop] + '</p>';
};
};
};
};
html += '<button class="addButton" data-id="' + item.id + '">Add to Basket</button>'
html += '</div>';
});
itemCon.innerHTML += html;
}
};
});
function init() {
shop.addItem("0", "coat", "Warm yes!", 24.99);
shop.print;
};
window.onload = init;
var-shop=(函数(){
var项目=[];
返回{
项目:功能(数据){
this.id=数据[0];
此项=数据[1];
此.description=数据[2];
该价格=数据[3];
this.count=数据[4];
},
附加项:功能(id、项目、说明、价格){
items.push(此.Item(id、Item、description、price));
},
打印:函数(){
var itemCon=document.getElementById('items'),
html=“”;
items.forEach(功能(项目){
html+='';
用于(项目中的道具){
如果(道具!=“id”){
如果(道具!=“计数”){
如果(价格){
html+=''+prop+':“+item[prop]+'”;
}否则{
html+=''+prop+':'+item[prop]+'';
};
};
};
};
html+=“添加到篮子”
html+='';
});
itemCon.innerHTML+=html;
}
};
});
函数init(){
商店补充(“0”,“外套”,“温暖的是!”,24.99);
商店印刷;
};
window.onload=init;
您没有调用IIFE,因此它没有返回您想要的对象
var shop = (function() {
var items = [];
return {
// your code
};
})(); // <-- Invoke it!
var-shop=(函数(){
var项目=[];
返回{
//你的代码
};
})(); //