0

javascriptでクラスを作ろうとしています。

define(['knockout', 'knockout-validation', 'jquery'], function(ko, validation, $) {
    var SignUpViewModel = {
        name: ko.observable().extend({
            required: true
        }),
        email: ko.observable().extend({
            required: true,
            email: true
        }),
        password: ko.observable().extend({
            required: true
        }),
        confirmPassword: ko.observable().extend({
            areSame: {
                params: password,
                message: "Repeat password must match Password"
            }
        }), // this line contains error . 
        register: function() {}
    }
    return SignUpViewModel;
});

undefinedパスワードでエラーが発生するようになりました

前もって感謝します。

4

2 に答える 2

1

オブジェクト リテラルは、クラスの作成には最適ではありません。しかし、それらは強力なツールです。このように行うことができます

(function(app) {
    app.define = function (definition) {
        definition.prototype = definition.prototype || {};
        definition.init.prototype = definition.prototype;
        definition.init.prototype.constructor = definition.init;

        return definition.init;
    };

})(window.app = window.app || {});

のように使う

app.define({
   init: function() {
        this.password = ko.observable().extend({ required: true });

        this.confirmPassword = ko.observable().extend({
            areSame: {
                params: this.password,
                message: "Repeat password must match Password"
            }
        });
   },
   prototype: {
      register: function() {
      }
   }
});

http://jsfiddle.net/ak2Ej/

于 2013-08-15T12:01:48.107 に答える
1

をどのように呼び出しているかは述べていませんがcallitfunction、次のような場合:

mytestobj.callitfunction();

...その後this.password、呼び出し内で定義されます。

console.log("The password is " + this.password()); // Since password is a KO observable, it's a function, so use () on it

または、これは 1 回限りのオブジェクトであるため、単に を使用しますmytestobj.password。例えば:

console.log("The password is " + mytestobj.password());

...そして、あなたはに依存していませんthis

thisJavaScript 関数内での呼び出しは、他の言語のように関数が定義されている場所ではなく、主に関数の呼び出し方法によって決定されることに注意してください。したがって、たとえば、ここにthisはありません。mytestobj

var f = mytestobj.callitfunction;
f(); // `this` is not `mytestobj` within the call

もっと:

于 2013-08-15T09:29:31.170 に答える