From da6ebeccb2c127c8cc70e955119effb346473b85 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 24 Feb 2016 16:33:58 +0800 Subject: [PATCH] improves method definition for CCClass --- cocos2d/core/components/CCComponentInSG.js | 1 + cocos2d/core/components/CCSprite.js | 2 +- cocos2d/core/platform/CCClass.js | 53 ++++++++++------------ cocos2d/deprecated.js | 47 +++++++++---------- 4 files changed, 51 insertions(+), 52 deletions(-) diff --git a/cocos2d/core/components/CCComponentInSG.js b/cocos2d/core/components/CCComponentInSG.js index 98c69b5330..a96dc95c8f 100644 --- a/cocos2d/core/components/CCComponentInSG.js +++ b/cocos2d/core/components/CCComponentInSG.js @@ -6,6 +6,7 @@ var SceneGraphHelper = require('../utils/scene-graph-helper'); * * You should override: * - _createSgNode + * - _initSgNode * * @class _ComponentInSG * @extends Component diff --git a/cocos2d/core/components/CCSprite.js b/cocos2d/core/components/CCSprite.js index 8e16182db7..2e0093eb7b 100644 --- a/cocos2d/core/components/CCSprite.js +++ b/cocos2d/core/components/CCSprite.js @@ -92,7 +92,7 @@ var Sprite = cc.Class({ * @type {SpriteAtlas} */ _atlas: { - default: '', + default: null, type: cc.SpriteAtlas, tooltip: 'i18n:COMPONENT.sprite.atlas', editorOnly: true, diff --git a/cocos2d/core/platform/CCClass.js b/cocos2d/core/platform/CCClass.js index f640cd8124..63cac354ae 100644 --- a/cocos2d/core/platform/CCClass.js +++ b/cocos2d/core/platform/CCClass.js @@ -583,35 +583,26 @@ function _boundSuperCall (func, funcName, base) { var superFunc = null; var pd = JS.getPropertyDescriptor(base.prototype, funcName); if (pd) { - if (pd.value) { - if (typeof pd.value === 'function') { - superFunc = pd.value; + superFunc = pd.value; + // ignore pd.get, assume that function defined by getter is just for warnings + if (typeof superFunc === 'function') { + var hasSuperCall = SuperCallReg.test(func); + if (hasSuperCall) { + return function () { + var tmp = this._super; + + // Add a new ._super() method that is the same method but on the super-Class + this._super = superFunc; + + var ret = func.apply(this, arguments); + + // The method only need to be bound temporarily, so we remove it when we're done executing + this._super = tmp; + + return ret; + }; } } - else if (pd.get) { - var got = pd.get(); - if (typeof got === 'function') { - superFunc = got; - } - } - } - if (superFunc) { - var hasSuperCall = SuperCallReg.test(func); - if (hasSuperCall) { - return function () { - var tmp = this._super; - - // Add a new ._super() method that is the same method but on the super-Class - this._super = superFunc; - - var ret = func.apply(this, arguments); - - // The method only need to be bound temporarily, so we remove it when we're done executing - this._super = tmp; - - return ret; - }; - } } return null; } @@ -781,7 +772,13 @@ function CCClass (options) { } var func = options[funcName]; if (typeof func === 'function' || func === null) { - cls.prototype[funcName] = func; + // use defineProperty to redefine some super method defined as getter + Object.defineProperty(cls.prototype, funcName, { + value: func, + enumerable: true, + configurable: true, + writable: true, + }); } else if (CC_DEV) { var correct = TYPO_TO_CORRECT[funcName]; diff --git a/cocos2d/deprecated.js b/cocos2d/deprecated.js index 22488525fa..f7e6a2ce6a 100644 --- a/cocos2d/deprecated.js +++ b/cocos2d/deprecated.js @@ -298,26 +298,6 @@ if (CC_DEV) { } } - function shouldNotUseNodeProp (component) { - var compName = cc.js.getClassName(component); - var Info = 'Sorry, ' + compName + '.%s is removed, please use cc.Node.%s instead.'; - var compProto = component.prototype; - for (var prop in cc.Node.prototype) { - (function (prop) { - if (!(prop in compProto) && prop[0] !== '_') { - js.getset(compProto, prop, - function () { - cc.error(Info, prop, prop); - }, - function () { - cc.error(Info, prop, prop); - } - ); - } - })(prop); - } - } - // cc.Node markAsRemoved(cc.Node, [ @@ -407,8 +387,31 @@ if (CC_DEV) { removeAllComponents: 'removeComponent', getNodeToParentAffineTransform: 'getNodeToParentTransform', }); + + // RENDERERS - // cc.SpriteRenderer + function shouldNotUseNodeProp (component) { + var compProto = component.prototype; + for (var prop in cc.Node.prototype) { + (function (prop) { + if (!(prop in compProto) && prop[0] !== '_') { + Object.defineProperty(compProto, prop, { + get: function () { + var compName = cc.js.getClassName(this); // 允许继承 + var Info = 'Sorry, ' + compName + '.%s is undefined, please use cc.Node.%s instead.'; + cc.error(Info, prop, prop); + }, + enumerable: false, + configurable: true, // 允许继承 + }); + } + })(prop); + } + } + shouldNotUseNodeProp(cc._ComponentInSG); + + + // cc.Sprite markAsRemoved(cc.Sprite, [ 'textureLoaded', @@ -440,7 +443,6 @@ if (CC_DEV) { getSpriteFrame: 'spriteFrame', setSpriteFrame: 'spriteFrame', }); - shouldNotUseNodeProp(cc.Sprite); // Particle markAsRemoved(cc.ParticleSystem, [ @@ -490,7 +492,6 @@ if (CC_DEV) { '*etTotalParticles': 'totalParticles', '*etTexture': 'texture', }); - shouldNotUseNodeProp(cc.ParticleSystem); js.obsoletes(cc.ParticleSystem, 'cc.ParticleSystem', { Type: 'PositionType', Mode: 'EmitterMode'