第一个使用this
(第 2 行)错误的语句:
this is undefined
验证行号。验证了这种使用方式在另一个名为 SMessage 的函数中有效。尝试更改参数名称,使其不等于对象属性。无法弄清楚为什么它不会分配给 this.tag_array。已验证 tag_array1 已填充为值数组。
var VBPaneInit = function ( tag_array1 ) {
this.tag_array = tag_array1;
this.initializeTags();
this.initializePages();
VBPaneInit.prototype.initializeTags = function( ) {
var element;
for ( element = 0; element < this.tag_array.length; element++ )
{
document.getElementById( this.tag_array[element] ).onclick = vDomPageFlip;
}
};
VBPaneInit.prototype.initializePages = function( ) {
if ( this.tag_array.length === 0 )
{
return;
}
var index, page_element;
for ( index = 0; index < this.tag_array.length; index++ )
{
page_element = document.getElementById( this.tag_array[index] + '_page' );
page_element.style.display = 'none';
}
vDomPageFlip( this.tag_array[0] );
};
};
召唤
VBPaneInit( tag_array );
出于测试目的,减少到:
var VBPaneInit = function( tag_array ) {
this.tag_array = tag_array;
}
还是不行。
工作类似:
var SMessage = function ( element ) {
this.element = element;
SMessage.prototype.display = function( type ) {
this.element.innerHTML = this.messages[ type ];
};
SMessage.prototype.messages = {
name: 'Please enter a valid name',
email: 'Please enter a valid email',
pass: 'Please enter password, 6-40 characters',
url: 'Please enter a valid url',
title: 'Please enter a valid title',
tweet: 'Please enter a valid tweet',
empty: 'Please complete all fields',
same: 'Please make emails equal',
taken: 'Sorry, that email is taken',
validate: 'Please contact <a class="d" href="mailto:chris@host.com">support</a> to reset your password',
s_name: 'Please enter a valid name.',
s_email: 'Please enter a valid email.',
s_pass: 'Please enter password, 6-40 characters.',
s_url: 'Please enter a valid url.',
s_title: 'Please enter a valid title.',
s_tweet: 'Please enter a valid tweet.',
s_empty: 'Please complete all fields.',
s_same: 'Please make emails equal.',
s_taken: 'Sorry, that email is taken.',
s_validate: 'Please contact <a class="d" href="mailto:chris@host.com">support</a> to reset your password.'
};
};