使用模块模式调用函数属性

时间:2014-12-19 21:24:10

标签: javascript properties

在下面的代码中,我想问一下调用seqer.set_prefix = ('Q');seqer.set_prefix('Q');之间有什么区别?

var serial_maker = function ( ) {
    // Produce an object that produces unique strings. A
    // unique string is made up of two parts: a prefix
    // and a sequence number. The object comes with
    // methods for setting the prefix and sequence

    // number, and a gensym method that produces unique
    // strings.
    var prefix = '';
    var seq = 0;

    return {
            set_prefix: function (p) {
            prefix = String(p);
        },

        set_seq: function (s) {
            seq = s;
        },
        gensym: function ( ) {
                var result = prefix + seq;
                seq += 1;
                return result;
            }
        };
    };


    var seqer = serial_maker();
    seqer.set_prefix = ('Q'); // OR seqer.set_prefix('Q');
    seqer.set_seq = (1000); // OR seqer.set_seq(1000);;
    var unique = seqer.gensym(); // unique is "Q1000"

1 个答案:

答案 0 :(得分:2)

区别在于:

seqer.set_prefix = ('Q');

使用值set_prefix替换'Q'属性。

seqer.set_prefix('Q');

使用值set_prefix()调用'Q'方法。 (而这又将prefix变量的值设置为'Q'。)

同样的解释适用于set_seq行。