简体   繁体   English

在另一个内部访问一个javascript类

[英]Accessing one javascript class inside another

I have two javascript “classes”. 我有两个javascript“类”。 One of them is supposed to be a sort of general one, that will instantiate another sub-classes. 其中一个应该是一种通用的,它将实例化另一个子类。

Somehow this throws an undefined is not a function error: 不知怎的,抛出一个undefined is not a function错误:

    this.progress = new Uploader.Progress({
      matcher: options.matcher,
    });

I'm using underscore as a dependency included through the Rails asset pipeline require statement. 我使用下划线作为通过Rails资产管道require语句包含的依赖项。 Here is the full code: 这是完整的代码:

//= 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: "/users/1/videos",
});

u create an uploader object on module scope 你在模块范围上创建一个上传者对象

if (typeof Uploader === "undefined") {
var Uploader = {};
}

but then u create another local one 但后来你创造了另一个本地的

var Uploader = Project.Uploader = function(options) ...

binding anything on *this in the local object is not visible in the global one. 在本地对象中绑定任何内容* this在全局对象中不可见。 that is a very strange style. 这是一种非常奇怪的风格。

When you say 当你说

this.progress = new Uploader.Progress({
      matcher: options.matcher,
    });

it matched the Uploader defined in the function scope` which is 它匹配函数范围中Uploader defined in theUploader defined in the

var Uploader = Project.Uploader = function(options) {

and this one doesn't have a property Progress so Uploader.Progress is undefined . 并且这个没有属性Progress所以Uploader.Progressundefined Hence, the error. 因此,错误。

To fix that, change 要解决这个问题,请改变

var Uploader = Project.Uploader = function(options) {

To

var SomeOtherVariable = Project.Uploader = function(options) {

so now when you call new Uploader.Progress({ it will start looking for Uploader outside the function scope as it will not find it within the function scope. The correct function set for Uploader.Progress in the global scope would be called. 所以现在当你调用new Uploader.Progress({它将开始在函数范围之外寻找Uploader ,因为它在函数范围内找不到它。将调用全局范围内Uploader.Progress的正确函数集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM