声明后访问对象内部的javascript属性

声明后访问对象内部的javascript属性,javascript,object,knockout.js,requirejs,Javascript,Object,Knockout.js,Requirejs,我正在尝试用javascript创建一个类 define(['knockout', 'knockout-validation', 'jquery'], function(ko, validation, $) { var SignUpViewModel = { name: ko.observable().extend({ required: true }), email: ko.observable().extend(

我正在尝试用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;
});
现在它在密码处给出了
未定义的
错误


提前谢谢

您没有说明如何调用
callitfunction
,但如果是这样的话:

mytestobj.callitfunction();
…然后
此密码将在呼叫中定义

console.log("The password is " + this.password()); // Since password is a KO observable, it's a function, so use () on it
或者,由于这是一个一次性对象,只需使用
mytestobj.password
。例如:

console.log("The password is " + mytestobj.password());
…然后你就不再依赖于这个了

请注意,
在JavaScript函数调用中主要取决于函数的调用方式,而不是在某些其他语言中定义函数的位置。因此,例如,
这个
将不会是
mytestobj
这里:

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


对象文字对于类创建来说并不是最佳的。但它们是一个强大的工具,你可以这样做

(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() {
      }
   }
});

谢谢@T.J.Crowder的更正。我修复了它。使用
this.password
访问它。@ManojAwasthi
this
将是这里的窗口对象。所以
这个
不起作用了我解释了我的问题并告诉了我的真实情况。这样我可以得到更具体的帮助。请参阅我的编辑。