1

在下面的消息对象中,如果我这样称呼它:

var message_object = new Message( response_element );
message_object.display( 'empty' );

是否为每次调用 new Message() 创建了消息数组...如果是这样,我如何确保只有一个消息数组,因为这就是我所需要的?

var Message = function( element )
{
    var messages =  
    {
        name:         'Please enter a valid name',
        email:        'Please enter a valid email',
        pass:         'Please enter passoword, 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',
        email_s:      'Please enter a valid email.',
        same:         'Please make emails equal',
        taken:        'Sorry, that email is taken',
        validate:     'Please contact <a class="d" href="mailto:chris@domain.com">support</a> to reset your password',
    }
    this.display = function( type ) 
    {
        element.innerHTML = messages[ type ];
        new Effects().fade( element, 'down', 4000 );
    }
};
4

2 回答 2

3

是的,每次都会执行整个函数,并且每次都会重新定义消息对象和函数。

您可能想要做的是将它包装在一个闭包中,并利用 Javascript 的原型继承

var Message = (function () {
    var messages = {
        name:         'Please enter a valid name',
        email:        'Please enter a valid email',
        pass:         'Please enter passoword, 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',
        email_s:      'Please enter a valid email.',
        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',
    };
    var Message = function (element) {
        this.element = element;
    };

    Message.prototype.display = function( type ) {
        this.element.innerHTML = messages[ type ];
        new Effects().fade( this.element, 'down', 4000 );
    };
    return Message;
}());

这使messages对象对“类”保持私有。

于 2012-04-10T22:26:22.250 回答
1

messages是一个对象而不是数组,但是是的:每次调用您的Message()函数时都会创建一个新对象。

如果您只需要一个副本,那么在其他语言中什么是“静态”变量,一种方法是执行以下操作:

var Message = function( element )
{
    this.display = function( type ) 
    {
        element.innerHTML = this.messages[ type ];
        new Effects().fade( element, 'down', 4000 );
    }
};

Message.prototype.messages = {
    name:         'Please enter a valid name',
    email:        'Please enter a valid email',
    pass:         'Please enter passoword, 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',
    email_s:      'Please enter a valid email.',
    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',
};

prototype所有实例都可以访问添加到 的属性和方法- 请注意,在display()方法中它以this.messages.

(请注意,在原型上定义您的方法也可能是明智的,这将涉及使用 . 制作参数display()的实例副本。)elementthis.element = element;

于 2012-04-10T22:24:25.600 回答