1
我有一个网页链接的JavaScript对象:为什么我的锚不能触发这个JavaScript对象?
//the constructor function
function NewsScroller() {
}
//now put some config objects using the JSON structure
NewsScroller.prototype.config = {
serviceUrl : '/NewsProvider.svc/rest/GetNews/',
pageIndex : 0
}
//the argumented constuctor for this object
NewsScroller.prototype.init = function() {
this.getNews(this.config.pageIndex);
console.log(this.config.pageIndex);
}
NewsScroller.prototype.decreasePage = function() {
console.log('current page index ' + this.config.pageIndex);
}
然后我在页面准备好声明:
<script>
$(document).ready(function() {
var newsScrollerForPage = new NewsScroller();
newsScrollerForPage.init();
newsScrollerForPage.decreasePage();
});
</script>
其产生的结果:
当前页面的索引0
我想调用好玩的从一个锚标记ction所以我有:
<div class="scroller-left">
<a id="scroller-left-a" href="javascript:newsScrollerForPage.decreasePage();">
<img src="/Images/Left-Scroller.jpg"/>
</a>
</div>
但是当我点击锚我得到:
newsScrollerForPage没有定义
这是为什么?当然,我应该能够像在.ready方法中那样调用对象和函数?
我认为,因为封闭,那么页面将允许访问该函数内的变量,即使它返回? – Exitos
但是,你的权利只是关于这件事情是如何工作的?它只是一个可以访问的函数吗? – Exitos
看到我关于范围的编辑。 – ADW