Javascript 是什么导致此代码无法工作?
我试图理解为什么这不起作用。(尚未验证的基本示例) 当我测试它时,firebug声明找不到Product.addPageJavascript 是什么导致此代码无法工作?,javascript,closures,Javascript,Closures,我试图理解为什么这不起作用。(尚未验证的基本示例) 当我测试它时,firebug声明找不到Product.addPage var Product = function () { var Page = function () { var IMAGE = ''; return { image : function () { return IMAGE;
var Product = function ()
{
var Page = function ()
{
var IMAGE = '';
return {
image : function ()
{
return IMAGE;
},
setImage : function (imageSrc_)
{
IMAGE = '<img id="image" src="' + imageSrc_ + '" height="100%" width="100%">';
}
};
};
var PAGES = [];
return {
addPage : function ()
{
var len = PAGES.length + 1;
PAGES[len] = new Page();
return PAGES[len];
},
page : function (pageNumber_)
{
var result = PAGES[pageNumber_];
return result;
}
};
};
// Begin executing
$(document).ready(function ()
{
Product.addPage.setImage('http://site/images/small_logo.png');
alert(Product.page(1).image());
});
var乘积=函数()
{
变量页=函数()
{
var图像=“”;
返回{
图像:函数()
{
返回图像;
},
setImage:function(imageSrc)
{
图像='';
}
};
};
var页面=[];
返回{
addPage:函数()
{
var len=PAGES.length+1;
页面[len]=新页面();
返回页[len];
},
页码:功能(页码)
{
var结果=页数[页码];
返回结果;
}
};
};
//开始执行
$(文档).ready(函数()
{
Product.addPage.setImage('http://site/images/small_logo.png');
警报(Product.page(1.image());
});
关于Product.addPage
()
.setImage('http://site/images/small_logo.png');代码>
编辑:结果我只发现了一半的问题。看看dtsazza对整个问题的回答。关于Product.addPage
()
.setImage('http://site/images/small_logo.png');代码>
编辑:结果我只发现了一半的问题。查看dtsazza的答案,了解全部内容。您试图引用Product函数的addPage属性(在本例中,它是一个构造函数),而不是在返回的对象上
您可能需要以下内容:
// Begin executing
$(document).ready(function ()
{
var product = new Product();
product.addPage().setImage('http://site/images/small_logo.png');
alert(product.page(1).image());
});
//开始执行
$(文档).ready(函数()
{
var product=新产品();
product.addPage().setImage(“”);
警报(product.page(1.image());
});代码>
它还将括号添加到addPage调用中(尽管FireBug不会一直抱怨这个问题,因为它无论如何都无法找到该方法)。您试图引用Product函数的addPage属性(在本例中,它是一个构造函数),而不是在返回的对象上
您可能需要以下内容:
// Begin executing
$(document).ready(function ()
{
var product = new Product();
product.addPage().setImage('http://site/images/small_logo.png');
alert(product.page(1).image());
});
//开始执行
$(文档).ready(函数()
{
var product=新产品();
product.addPage().setImage(“”);
警报(product.page(1.image());
});代码>
它还将括号添加到addPage调用中(尽管FireBug不会一直抱怨这个问题,因为它无论如何都无法找到该方法)。这也会起作用:
Product().addPage().setImage('http://site/images/small_logo.png');
这也会起作用:
Product().addPage().setImage('http://site/images/small_logo.png');
那么:
var Product = {
Page : function() {
return {
_image : '',
Image : function() {
return this._image;
},
setImage : function(imageSrc_) {
this._image = '<img id="image" src="' + imageSrc_ + '" height="100%" width="100%">';
}
};
},
Pages : [],
addPage : function() {
var Page = new Product.Page();
this.Pages.push(Page);
return Page;
},
GetPage : function(pageNumber_) {
return this.Pages[pageNumber_];
}
};
// Begin executing
$(document).ready(function ()
{
Product.addPage().setImage('http://site/images/small_logo.png');
alert(Product.GetPage(0).Image());
});
var乘积={
页面:函数(){
返回{
_图像:“”,
图像:函数(){
返回此。\u图像;
},
setImage:function(imageSrc){
这个._image='';
}
};
},
页码:[],
addPage:function(){
var Page=新产品.Page();
此.Pages.push(页面);
返回页面;
},
GetPage:函数(页码){
返回此。页码[页码];
}
};
//开始执行
$(文档).ready(函数()
{
Product.addPage().setImage('http://site/images/small_logo.png');
警报(Product.GetPage(0.Image());
});
那么:
var Product = {
Page : function() {
return {
_image : '',
Image : function() {
return this._image;
},
setImage : function(imageSrc_) {
this._image = '<img id="image" src="' + imageSrc_ + '" height="100%" width="100%">';
}
};
},
Pages : [],
addPage : function() {
var Page = new Product.Page();
this.Pages.push(Page);
return Page;
},
GetPage : function(pageNumber_) {
return this.Pages[pageNumber_];
}
};
// Begin executing
$(document).ready(function ()
{
Product.addPage().setImage('http://site/images/small_logo.png');
alert(Product.GetPage(0).Image());
});
var乘积={
页面:函数(){
返回{
_图像:“”,
图像:函数(){
返回此。\u图像;
},
setImage:function(imageSrc){
这个._image='';
}
};
},
页码:[],
addPage:function(){
var Page=新产品.Page();
此.Pages.push(页面);
返回页面;
},
GetPage:函数(页码){
返回此。页码[页码];
}
};
//开始执行
$(文档).ready(函数()
{
Product.addPage().setImage('http://site/images/small_logo.png');
警报(Product.GetPage(0.Image());
});
它会在那一行上,但下一行将无法在同一个产品实例上调用第(1)页
,它需要调用该实例。因此,需要存储为局部变量。它将位于该行上,但下一行将无法在需要调用的同一个product实例上调用page(1)
。因此需要存储为局部变量。感谢您的解释。我知道我错在哪里了。谢谢你的解释。我知道我现在错在哪里了。