我在创建构造函数属性时遇到问题。在萤火虫控制台中,错误说"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>