[英]Javascript scope issue - variable is undefined
I am slightly confused by this, as i am sure that all variables are taken to the 'top' of the javascript before run time and then processed from there. 我对此感到有些困惑,因为我确信所有变量在运行时都被带到javascript的“顶部”,然后从那里进行处理。
So my error 所以我的错误
TypeError: hutber.portfolio.ko is undefined
[Break On This Error]
items: ko.observableArray(hutber.portfolio.ko.data),
Object 宾语
(function ($) {
"use strict"; //For good development standards :)
hutber.portfolio = {
init: function(){
e(typeof(hutber));
hutber.portfolio.changeOptionsBoxHeight();
//Bind the height resize in window resize
$(window).resize(function(){
hutber.portfolio.changeOptionsBoxHeight();
});
//start KO
hutber.portfolio.ko.init();
},
changeOptionsBoxHeight: function(){
var height = $(window).height();
$('.portfolio--options').height(height-400);
}
};
hutber.portfolio.ko = {
init: function(){
ko.applyBindings(new hutber.portfolio.ko.portfolioViewModel());
},
data: [],
items: ko.observableArray(hutber.portfolio.ko.data),
portfolioViewModel: function(){
hutber.portfolio.ko.items = ko.observableArray(hutber.portfolio.ko.data);
$.getJSON('/js/pages/portfolio.json').done(function(info){
hutber.portfolio.ko.data = info;
hutber.portfolio.ko.items (hutber.portfolio.ko.data);
});
}
};
hutber.portfolio.init();
})(jQuery);
I really wanted to upload this to a fiddle but for some reason, i'm getting js errors on their site. 我确实想将其上传到一个小提琴,但是由于某种原因,我在他们的网站上遇到了js错误。 I believe my firewall is blocking certain files from loading.
我相信我的防火墙阻止了某些文件的加载。
At the point ko.observableArray(hutber.portfolio.ko.data)
is run, hutber.portfolio.ko
has not be defined yet. 在运行
ko.observableArray(hutber.portfolio.ko.data)
,尚未定义hutber.portfolio.ko
。
You can work around it like that: 您可以像这样解决它:
hutber.portfolio.ko = {
init: function(){
ko.applyBindings(new hutber.portfolio.ko.portfolioViewModel());
},
data: [],
portfolioViewModel: function(){
hutber.portfolio.ko.items = ko.observableArray(hutber.portfolio.ko.data);
$.getJSON('/js/pages/portfolio.json').done(function(info){
hutber.portfolio.ko.data = info;
hutber.portfolio.ko.items (hutber.portfolio.ko.data);
});
}
};
hutber.portfolio.ko.items = ko.observableArray(hutber.portfolio.ko.data);
But at this point hutber.portfolio.ko.data
is always []
. 但是此时
hutber.portfolio.ko.data
始终为[]
。 So you might as well put ko.observableArray([])
in your original code. 因此,您最好将
ko.observableArray([])
放入原始代码中。
只需猜测:因为您在声明变量之前就访问了它?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.