简体   繁体   English

Javascript类-调用函数时未定义不是函数

[英]Javascript Class - undefined is not a function when calling function

I´m trying to program OO in javascript. 我正在尝试用JavaScript编程OO。 There are several variants of doing this. 有几种变体。

I have seen something I think in some AnglularJS examples which is for me the clearest one. 我在一些AnglularJS示例中看到了一些我认为最清晰的示例。

Sadly I´m getting 可悲的是我

undefined is not a function 未定义不是函数

when calling a function of my instance 在调用我的实例的函数时

var MyChooser = (function () {

    var self = this;
    var constructor = function (selectBox, previewBox) {
        this.selectBox = selectBox;
        this.previewBox = previewBox;
        this.selectBox.change(function () {
            alert(this.value);
        })
    };

    MyChooser.prototype.loadFrames = function () {
        alert("load");
        $.ajax({url: "api.php"}).done(function (data) {
            alert(data);
            //self.frames = data;
        })
    };

    MyChooser.prototype.displayFrame = function (frame) {
        this.previewBox.setAttribute("src", frame.src);
    };

    MyChooser.prototype.invalidateSelectBox = function () {

    };

    return constructor;
});



 var fp = new MyChooser(document.getElementById("frame-select"), document.getElementById("frame-preview"));
            fp.loadFrames();

Why do I get this error? 为什么会出现此错误?

(I also removed the jquery calls, but it is not the problem) (我也删除了jquery调用,但这不是问题)

You lack a () at the end to make the enclosing function an IIFE. 您最后没有()使封闭函数成为IIFE。

var MyChooser = (function () {

  var constructor = function(select,...stuff){...}

  // Note we're using constructor.prototype rather than MyChooser.prototype
  constructor.prototype.foo = function...
  ...

  return constructor;
}()); // Note the ()

Without it, your constructor won't be assigned/returned to MyChooser . 没有它,您的constructor将不会被分配/返回给MyChooser You end up with MyChooser being a function that returns constructor , rather than constructor itself. 最后, MyChooser是一个返回constructor函数的函数,而不是返回constructor constructor本身的constructor Also, change your prototype methods to point to constructor instead of MyChooser . 另外,将原型方法更改为指向constructor而不是MyChooser

Also, all this confusion reasulted from the fact that the constructor and it's method definitions are placed in a closure - which is not necessary. 同样,所有这些混淆是由于构造函数及其方法定义放在闭包中这一事实而导致的-这是不必要的。 You can simply go with: 您可以简单地选择:

function MyChooser(select,...stuff){...}
MyChooser.prototype.moarStuff = function(){...};

...

var fp = new MyChooser(...);

This can work: 这可以工作:

 var MyChooser = (function () {
    function MyChooser (p1, p2) {

    }

    MyChooser.prototype.f1 = function () {
        return true;
    };

    return MyChooser 
})();

var thisChooser = new MyChooser(param1, param2);

Notice the iife, im returning MyChooser instead of the constructor, and how the constructor is defined. 请注意,iife返回MyChooser而不是构造函数,以及构造函数的定义方式。

Since you mentioned that you're doing OO in Javascript, let me introduce you to a better way of creating objects in javascript that is cleaner, easier to read, and safer . 既然您提到您正在用Java进行OO,那么让我向您介绍一种更干净,更易读, 更安全的用javascript创建对象的更好方法。 I learned it while reading the excellent book "Javascript: The Good Parts" 我在阅读优秀的著作《 Javascript:好的部分》时学到了它

Here is how you can create your object. 这是创建对象的方法。

var constructor = function (spec) {
    var that;

    var privateFunc = function () {};
    var privateVariables = ...;

    var publicMethod = function () {};

    that = Object.create({
        publicMethod: publicMethod,
        // public methods and properties
    });
    return that;
};

Now whenever you need an object, just do 现在,只要您需要一个物体,就要做

var newObject = constructor();

This is much more flexible and cleaner IMHO. 这是更加灵活和清洁的恕我直言。 And it saves you from accidentally polluting global scope if you ever forget to type the new keyword. 如果您忘记键入new关键字,它可以避免意外污染全局范围。

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

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