Quantcast
Channel: Cocos中文社区 - 最新主题
Viewing all articles
Browse latest Browse all 88737

看到 一个以前没见过的js的语法,这个叫什么呢?

$
0
0

@734589166 写道:

这是一段cocos-js里的源代码,在CCMenuItem.js文件里:
ctor: function (normalImage, selectedImage, three, four, five) {
var normalSprite = null,
selectedSprite = null,
disabledSprite = null,
callback = null,
target = null;

    if (normalImage === undefined || normalImage === null) {
        cc.MenuItemSprite.prototype.ctor.call(this);
    }
    else {
        normalSprite = new cc.Sprite(normalImage);
        selectedImage &&
        (selectedSprite = new cc.Sprite(selectedImage));
        if (four === undefined) {
            callback = three;
        }
        else if (five === undefined) {
            callback = three;
            target = four;
        }
        else if (five) {
            disabledSprite = new cc.Sprite(three);
            callback = four;
            target = five;
        }
        cc.MenuItemSprite.prototype.ctor.call(this, normalSprite, selectedSprite, disabledSprite, callback, target);
    }
},

主要是这一行:

这个写法很有意思:

  selectedImage &&
  (selectedSprite = new cc.Sprite(selectedImage));

我的理解是这样,这里等同下面的代码:

if(selectedImage !== undefind && selectedImage !== null){
    selectedSprite = new cc.Sprite(selectedImage);
}

就是先判断是否为空,然后再进行后面的操作。那以后要做undefined和null的判断的时候这样写可就省事了。

帖子: 2

参与者: 1

阅读整个主题


Viewing all articles
Browse latest Browse all 88737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>