mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-03 17:42:41 +08:00
Merge pull request #381 from jareguo/master
improves method definition for CCClass
This commit is contained in:
@@ -6,6 +6,7 @@ var SceneGraphHelper = require('../utils/scene-graph-helper');
|
||||
*
|
||||
* You should override:
|
||||
* - _createSgNode
|
||||
* - _initSgNode
|
||||
*
|
||||
* @class _ComponentInSG
|
||||
* @extends Component
|
||||
|
||||
@@ -92,7 +92,7 @@ var Sprite = cc.Class({
|
||||
* @type {SpriteAtlas}
|
||||
*/
|
||||
_atlas: {
|
||||
default: '',
|
||||
default: null,
|
||||
type: cc.SpriteAtlas,
|
||||
tooltip: 'i18n:COMPONENT.sprite.atlas',
|
||||
editorOnly: true,
|
||||
|
||||
@@ -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];
|
||||
|
||||
+24
-23
@@ -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'
|
||||
|
||||
Reference in New Issue
Block a user