@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