mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-04 10:32:39 +08:00
Merge pull request #143 from jareguo/v0.7
throw warning if property overrided without specifing `override` keyword
This commit is contained in:
@@ -126,6 +126,7 @@ var Sprite = cc.Class({
|
||||
return cc.size(sgNode.width, sgNode.height);
|
||||
},
|
||||
visible: false,
|
||||
override: true
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -718,7 +718,7 @@ function FireClass (options) {
|
||||
if (properties) {
|
||||
|
||||
// 预处理属性
|
||||
preprocessAttrs(properties, name);
|
||||
preprocessAttrs(properties, name, cls);
|
||||
|
||||
for (var propName in properties) {
|
||||
var val = properties[propName];
|
||||
|
||||
@@ -146,10 +146,23 @@ function postCheckType (val, type, className, propName) {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function (properties, className) {
|
||||
function getBaseClassWherePropertyDefined (propName, cls) {
|
||||
if (CC_DEV) {
|
||||
var res;
|
||||
for (; cls && cls.__props__ && cls.__props__.indexOf(propName) !== -1; cls = cls.$super) {
|
||||
res = cls;
|
||||
}
|
||||
if (!res) {
|
||||
cc.error('unknown error');
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = function (properties, className, cls) {
|
||||
for (var propName in properties) {
|
||||
var val = properties[propName];
|
||||
|
||||
|
||||
var isObj = val && typeof val === 'object' && !Array.isArray(val);
|
||||
var isLiteral = isObj && val.constructor === Object;
|
||||
if ( !isLiteral ) {
|
||||
@@ -158,6 +171,11 @@ module.exports = function (properties, className) {
|
||||
};
|
||||
}
|
||||
if (val) {
|
||||
if (!val.override && cls.__props__.indexOf(propName) !== -1 && CC_DEV) {
|
||||
// check override
|
||||
var baseClass = cc.js.getClassName(getBaseClassWherePropertyDefined(propName, cls));
|
||||
cc.warn('"%s.%s" hides inherited property "%s.%s". To make the current property override that implementation, add the `override: true` attribute please.', className, propName, baseClass, propName);
|
||||
}
|
||||
var notify = val.notify;
|
||||
if (notify) {
|
||||
parseNotify(val, propName, notify, properties);
|
||||
|
||||
@@ -71,7 +71,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{
|
||||
|
||||
// SERIALIZABLE
|
||||
|
||||
_name: '',
|
||||
_opacity: 255,
|
||||
_color: cc.Color.WHITE,
|
||||
_cascadeOpacityEnabled: true,
|
||||
|
||||
@@ -123,7 +123,8 @@ test('Inherit', function () {
|
||||
properties: {
|
||||
myName: {
|
||||
default: 'doge',
|
||||
tooltip: 'String'
|
||||
tooltip: 'String',
|
||||
override: true
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -187,7 +188,10 @@ test('Inherit + constructor', function () {
|
||||
name: 'cc.Dog',
|
||||
extends: Animal,
|
||||
properties: {
|
||||
myName: 'doge'
|
||||
myName: {
|
||||
default: 'doge',
|
||||
override: true
|
||||
},
|
||||
}
|
||||
});
|
||||
var Husky = cc.Class({
|
||||
@@ -317,7 +321,10 @@ test('isChildClassOf', function () {
|
||||
name: 'cc.Dog',
|
||||
extends: Animal,
|
||||
properties: {
|
||||
name: 'doge'
|
||||
name: {
|
||||
default: 'doge',
|
||||
override: true
|
||||
}
|
||||
}
|
||||
});
|
||||
var Husky = cc.Class({
|
||||
@@ -513,7 +520,10 @@ test('mixins', function () {
|
||||
extends: Dog,
|
||||
mixins: [Mixin1, Mixin2],
|
||||
properties: {
|
||||
p1: 'Defined by BigDog',
|
||||
p1: {
|
||||
default: 'Defined by BigDog',
|
||||
override: true
|
||||
},
|
||||
p4: 'Defined by BigDog',
|
||||
},
|
||||
stop: function () {},
|
||||
|
||||
Reference in New Issue
Block a user