0

以前は Modal は関数であり、このように定義されていました

function Modal (heading){
               this.modal="Hello"; //works fine
               Modal.prototype.show = function(){ // Not working
                     $('#exceptionModal').modal('show');
               }    
 }

私はそれをrequirejsモジュール用に変換しようとしました

define("wbModal", function(){
    return{
            Modal: function(heading){
                   this.modal="Hello"; //works fine
                   this.prototype.show = function(){ // Not working
                         $('#exceptionModal').modal('show');
                   }
            }
    }
}

何が問題なのかわかりません。this.modal機能する場合は、機能しないのはなぜthis.prototype.showですか?

以下はコンソールで見つけることができます:

Uncaught TypeError: Cannot set property 'show' of undefined 
4

3 に答える 3

3

エラー メッセージが示すとおりですthis.prototypeundefinedしたがって、もちろんプロパティを設定することはできません。匿名関数とコンストラクター関数を混同しています。

これを試して:

define("wbModal", function() {
    function ModalConstructor(heading) {
        this.modal = "Hello";
    }

    ModalConstructor.prototype.show = function() {
        // you'll probably want to use `this` in some way here
        $('#exceptionModal').modal('show');
    };

    return {
        Modal: ModalConstructor
    }
}

またはこれ:

define("wbModal", function() {

    return {
        Modal: function(heading) {
            this.modal = "Hello";
            this.show = function() {
                $('#exceptionModal').modal('show');
            }
        }
    }
}
于 2013-03-12T19:21:33.267 に答える
0

thisのプロトタイプから継承するオブジェクトですModal[[Prototype]]内部プロパティはありません。オブジェクトでメソッドを定義するだけです。

this.show = function() {

};
于 2013-03-12T19:23:03.323 に答える
0
this.prototype.show = function({ // Not working
                 $('#exceptionModal').modal('show');
           });

これは、入力する必要があるものです。ブレースを確認します。

于 2013-03-12T19:22:39.283 に答える