0

我在创建构造函数属性时遇到问题。在萤火虫控制台中,错误说"telekomApp.validation.run is not a function"。该this.run()函数应该是TelekomApp构造函数的属性,我希望它也可以作为telekomApp.validation.run(),但事实并非如此。

请问您能对此有所了解吗?

非常感谢

JS:

(function(global) {

var TelekomApp = function() {

    this.validation = function() {

        var userNameField = document.getElementById("username-field"),
            passwordField = document.getElementById("password-field"),
            usernameMsg = document.getElementById("username-msg"),
            passwordMsg = document.getElementById("password-msg");

        this.run = function(tag) { // this method doesn't work...
            var o = this;
        };
      };
  };

  // make global
  global.TelekomApp = TelekomApp;

  // instanciate
  var telekomApp = new TelekomApp;

  // initialise validation...
  telekomApp.validation.run(document.getElementById("login-form"));

}(window));

HTML:

<div id="login">
        <p id="message"></p>
        <form id="login-form" action="#" method="post">
            <fieldset>
                <legend>Please login using your username and password</legend>
                <ol>
                    <li>
                        <label for="username-field">Username <span>*</span></label>
                        <input type="text" id="username-field" placeholder="Type your username" />
                        <span id="username-msg"></span>
                    </li>

                    <li>
                        <label for="password-field">Password <span>*</span></label>
                        <input type="password" id="password-field" placeholder="Type your password" />
                        <span id="password-msg"></span>
                    </li>

                    <li id="button">
                        <input type="submit" id="login-btn" value="Log In" />
                    </li>
                </ol>

            </fieldset>
        </form>
    </div>
4

2 回答 2

3
(function(global) {

var Validation = function() { //constructor for Validation

        var userNameField = document.getElementById("username-field"),
            passwordField = document.getElementById("password-field"),
            usernameMsg = document.getElementById("username-msg"),
            passwordMsg = document.getElementById("password-msg");

        this.run = function(tag) { // this method doesn't work...
            var o = this;
        };
};

var TelekomApp = function() {

    this.validation = new Validation();
  };

  // make global
  global.TelekomApp = TelekomApp;

  // instanciate
  var telekomApp = new TelekomApp;

  // initialise validation...
  telekomApp.validation.run(document.getElementById("login-form"));

}(window));
于 2013-03-13T17:26:06.810 回答
1

telekomApp.validation.run被调用时将被添加到对象telekomApp.validation中。

this.validation = function() {将函数添加到对象但不调用它。而且this.run = function(tag) {直接runtelekomApp,不加telekomApp.validation。一个小的改变就可以了:

var Validation = function() {
  ...// your code here
}
this.validation = new Validation();
于 2013-03-13T17:32:01.657 回答