...不是 JavaScript {自定义代码} 中的函数
… is not a function in javascript {custom code}
拜托,有人能告诉我,他到底我在语法上做错了什么吗?
问题始于语句this.form.onsubmit
,我得到this.initData is not a function
。
谢谢。
var Contact_Form = function(element){
this.form = element;
this.errors = new Array();
this.invalid = new Array();
this.inSent = false;
this.name = new String();
this.email = new String();
this.message = new String();
this.initData = function()
{
this.name = this.getElementValue('contact-name');
this.email = this.getElementValue('contact-email');
this.message = this.getElementValue('contact-message');
}
this.form.onsubmit = function(event)
{
event.preventDefault();
this.initData();
if(this.verifyData())
this.send();
}
this.verifyData = function()
{
if(!this.isNameLength())
this.setError('name', 'Zadejte, prosím, jméno dlouhé maximálně 30 znaků.');
if(this.isProperEmail())
{
if(!this.isEmailLength())
this.setError('email', 'Váš e-mail smí obsahovat maximálně 50 znaků.');
}
else
this.setError('email', 'Zadejte, prosím, email v korektním formátu.');
if(!this.isMessageLength())
this.setError('name', 'Zadejte, prosím, zprávu v rozsahu 1-999 znaků.');
this.doInvalidFields();
if(0 == this.errors.length)
return true;
return false;
}
this.doInvalidFields = function()
{
if(this.invalid.length > 0)
{
for(var invalid in this.invalid)
this.getElement(invalid).setAttribute('aria-invalid', true);
}
}
this.setError = function(field, message)
{
this.errors.push(message);
this.invalid.push(field);
}
this.getElementValue = function(element) {
return this.getElement(element).value;
}
this.getElement = function(element) {
return document.getElementById(element);
}
this.getElementName = function() {
return this.getElement('contact-name');
}
this.getElementEmail = function() {
return this.getElement('contact-email');
}
this.getElementMessage = function() {
return this.getElement('contact-message');
}
this.isNameLength = function(){
return this.isLength(this.name, 1, 30);
}
this.isEmailLength = function(){
return this.isLength(this.email, 1, 50);
}
this.isMessageLength = function(){
return this.isLength(this.email, 1, 999);
}
this.isProperEmail = function() {
return this.email.match(/^(?:'w){1,100}@{1}(?:'w){1,100}(?:.){1}(?:'w){1,10}$/ig);
}
this.isLength = function isLength(string, _min, _max) {
if(string.length >= _min && string.length <= _max)
return true;
return false;
}
}
window.onload = function()
{
new Contact_Form(document.forms[0]);
}
问题是this
不是继承的,并且在每个函数中具有不同的值。
然后,使用
var Contact_Form = function(element){
/* ... */
var that = this;
/* Here this===that */
this.form.onsubmit = function(event)
{
/* Here this===that.form */
event.preventDefault();
that.initData();
if(that.verifyData())
this.send();
}
/* ... */
}
this
指的是onsubmit
处理程序中的form
。您可以将this
赋值给局部变量,或使用 Function.prototype.bind
将处理程序绑定到正确的this
,即:
this.form.onsubmit = function(event) {
event.preventDefault();
this.initData();
if(this.verifyData())
this.send();
}.bind(this)
或与jQuery.proxy
this.form.onsubmit = $.proxy(function(event) {
event.preventDefault();
this.initData();
if(this.verifyData())
this.send();
}, this);
这两个示例都强制函数的this
上下文在调用处理程序时成为Contact_Form
的实例
相关文章:
- 如何将函数包装在函数中以避免代码重复
- 从var向代码隐藏函数传递值
- 为函数代码编写测试
- 如何简化jquery动画函数代码
- 用于删除数组中的零的Javascript函数代码
- 使用JavaScript中的高阶函数概念,用Python编写纯函数代码
- JQuery each() 函数代码即使在集合中没有元素时也在运行
- javascript.获取javascript函数位置或获取函数代码
- 部分函数代码是't已执行
- 什么'这是jQuery下面函数代码中的错误
- 为什么新函数(代码)比直接执行相同的代码更快
- 如何使用javascript调用ascx函数代码?DotNetNuke
- JQuery函数代码运行不正常
- Node.JS:MongoDB更新回调返回结果,返回函数代码
- 用nodejs在一定时间后运行函数/代码
- Jquery只处理第一个函数/代码块
- 如何确保我的dom操作函数/代码在dom被渲染后被调用
- 在jQuery中获取现成函数代码下面的元素
- Jquery函数返回的函数代码不是值
- 什么是'$(这个)'包含在下面的javascript函数代码中