繁体   English   中英

如何理解这段代码中代码的含义和意图?

[英]how to understand the meaning and the intention of code in this piece of code?

任何人都可以解释function exports()之后代码的功能究竟是什么,为什么我应该这样做呢?

(function () {
    'use strict';

    function Card() {
        this.isFaceUp = false;
        this.isUnplayalbe = false;
    }

    Card.prototype = {
        contents: function (v) {
            if (v === undefined) {
                return this._contents;
            } else {
                return this._contents = v;
            }
        },

        match: function (otherCards) {
            var score = 0;

            if (Array.isArray(otherCards)) {
                otherCards.forEach(compare, this);
            } else {
                compare.call(this, otherCards);
            }

            function compare(card) {
                if (card.contents() === this.contents()) {
                    score = 1;
                }
            }
            return score;
        }
    };

    function exports() {
        return new Card();
    }
    exports.Card = Card;
    window.card = exports;
})();

您的代码将Card属性添加到window对象,因此该属性将是一个Function对象(意味着可执行),因为该Card属性已赋值Function类型的值(导出声明为函数并分配给Card),但Function对象本身也有属性卡是Card对象,可执行属性也可以返回新的Card对象,如果被调用....

  • window.Card是导出功能
  • exports函数每次调用都会返回新的Card对象,所以window.Card也会这样做( var card1 = window.Card()
  • exports功能有属性Card (我不知道为什么需要)
  • 所以window.Card.Card - 也是一个Card对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM