我有兩個JavaScript「類」。其中一個應該是一個普通的類,它會實例化另一個子類。在另一個內部訪問一個JavaScript類
不知怎的,這將引發一個undefined is not a function
錯誤:
this.progress = new Uploader.Progress({
matcher: options.matcher,
});
我使用下劃線作爲包括通過rails資產管道的依賴需要聲明。下面是完整的代碼:
//= require underscore
if (typeof Uploader === "undefined") {
var Uploader = {};
}
(function() {
var Progress = Uploader.Progress = function(options) {
options || (options = {});
if(options.matcher) this.$matcher = $(options.matcher);
this.initialize.apply(this, arguments);
};
_.extend(Progress.prototype, {}, {
initialize: function() {
this.listen();
},
listen: function() {
this.$matcher.on("fileuploadprogress", function(e, data) {
var progress = parseInt(data.loaded/data.total * 100, 10);
data.context.find(".upload-progress").css({ "width": progress + "%" });
});
return this;
},
});
})();
(function() {
var Uploader = Project.Uploader = function(options) {
options || (options = {});
if(options.url) this.url = options.url;
if(options.matcher) this.$matcher = $(options.matcher);
this.progress = new Uploader.Progress({
matcher: options.matcher,
});
this.initialize.apply(this, arguments);
};
_.extend(Uploader.prototype, {}, {
initialize: function() {
this.listen();
},
listen: function() {
var _this = this;
this.$matcher.fileupload({
url: this.url,
type: "POST",
dataType: "json",
add: function(e, data) {
data.context = _this.$matcher.closest("form");
data.submit()
.success(function(result, textStatus, jqXHR) {
console.log("submitted");
});
},
});
return this;
},
});
})();
var uploader = new Project.Uploader({
matcher: "#video_file",
url: "https://stackoverflow.com/users/1/videos",
});
JavaScript錯誤來源於哪個行號?我們可以看到需要使用underscore.js的代碼嗎? –
格雷格,第39行導致錯誤('this.progress = new Uploader.Progress({...'),第1行需要underscore.js。 – Adam
好的,我懷疑問題在於那個花哨的命名空間在'var Uploader = Project.Uploader = function(...)'中。 – Adam