diff --git a/cocos2d/core/assets/CCRawAsset.js b/cocos2d/core/assets/CCRawAsset.js index ffe9b0bb5b..967b84bbce 100644 --- a/cocos2d/core/assets/CCRawAsset.js +++ b/cocos2d/core/assets/CCRawAsset.js @@ -51,7 +51,7 @@ cc.RawAsset = cc.Class({ Object.defineProperty(this, '_uuid', { value: '', writable: true, - enumerable: false // avoid uuid being assigned to empty string during destroy, + // enumerable is false by default, to avoid uuid being assigned to empty string during destroy }); }, @@ -84,8 +84,8 @@ cc.RawAsset = cc.Class({ Object.defineProperty(cc.RawAsset, 'isRawAssetType', { value: function (ctor) { return cc.isChildClassOf(ctor, cc.RawAsset) && !cc.isChildClassOf(ctor, cc.Asset); - }, - enumerable: false + } + // enumerable is false by default }); module.exports = cc.RawAsset; diff --git a/cocos2d/core/components/CCComponent.js b/cocos2d/core/components/CCComponent.js index ad2b168481..241bc19a7f 100644 --- a/cocos2d/core/components/CCComponent.js +++ b/cocos2d/core/components/CCComponent.js @@ -241,12 +241,6 @@ var Component = cc.Class({ _Scene.AssetsWatcher.initComponent(this); } - // dont reset _id when destroyed - Object.defineProperty(this, '_id', { - value: '', - enumerable: false - }); - // Support for Scheduler this.__instanceId = cc.ClassManager.getNewInstanceId(); }, @@ -736,6 +730,23 @@ var Component = cc.Class({ } }, + _destruct: function () { + // The same as super but dont reset _id when destroyed + for (var key in this) { + if (this.hasOwnProperty(key) && key !== '_id') { + switch (typeof this[key]) { + case 'string': + this[key] = ''; + break; + case 'object': + case 'function': + this[key] = null; + break; + } + } + } + }, + _instantiate: function () { var clone = cc.instantiate._clone(this, this); clone.node = null; @@ -837,8 +848,8 @@ if (CC_EDITOR || CC_TEST) { // NON-INHERITED STATIC MEMBERS - Object.defineProperty(Component, '_inspector', { value: '', enumerable: false }); - Object.defineProperty(Component, '_icon', { value: '', enumerable: false }); + Object.defineProperty(Component, '_inspector', { value: '', writable: true }); + Object.defineProperty(Component, '_icon', { value: '', writable: true }); // COMPONENT HELPERS @@ -883,11 +894,11 @@ Object.defineProperty(Component, '_registerEditorProps', { break; case 'inspector': - Object.defineProperty(cls, '_inspector', { value: val }); + Object.defineProperty(cls, '_inspector', { value: val, writable: true }); break; case 'icon': - Object.defineProperty(cls, '_icon', { value: val }); + Object.defineProperty(cls, '_icon', { value: val, writable: true }); break; case 'menu': diff --git a/cocos2d/core/platform/CCObject.js b/cocos2d/core/platform/CCObject.js index eef4a96992..6f64e7237c 100644 --- a/cocos2d/core/platform/CCObject.js +++ b/cocos2d/core/platform/CCObject.js @@ -192,8 +192,7 @@ if (CC_EDITOR) { clearImmediate(deferredDestroyTimer); deferredDestroyTimer = null; } - }, - enumerable: false + } }); } diff --git a/cocos2d/core/platform/_CCClass.js b/cocos2d/core/platform/_CCClass.js index 489edf9486..e63979fac9 100644 --- a/cocos2d/core/platform/_CCClass.js +++ b/cocos2d/core/platform/_CCClass.js @@ -69,12 +69,13 @@ Class.extend = function (props) { var classId = ClassManager.getNewID(); ClassManager[classId] = _super; - // Copy the properties over onto the new prototype. We make function - // properties non-eumerable as this makes typeof === 'function' check + + // Copy the properties over onto the new prototype. We keep function + // properties non-eumerable as default, this makes typeof === 'function' check // unneccessary in the for...in loop used 1) for generating Class() // 2) for cc.clone and perhaps more. It is also required to make // these function properties cacheable in Carakan. - var desc = { writable: true, enumerable: false, configurable: true }; + var nonEnumerableDesc = { writable: true, configurable: true }; prototype.__instanceId = null; @@ -87,17 +88,15 @@ Class.extend = function (props) { } _Class.id = classId; - // desc = { writable: true, enumerable: false, configurable: true, - // value: XXX }; Again, we make this non-enumerable. - desc.value = classId; - Object.defineProperty(prototype, '__cid__', desc); + nonEnumerableDesc.value = classId; + Object.defineProperty(prototype, '__cid__', nonEnumerableDesc); // Populate our constructed prototype object _Class.prototype = prototype; // Enforce the constructor to be what we expect - desc.value = _Class; - Object.defineProperty(_Class.prototype, 'constructor', desc); + nonEnumerableDesc.value = _Class; + Object.defineProperty(_Class.prototype, 'constructor', nonEnumerableDesc); for(var idx = 0, li = arguments.length; idx < li; ++idx) { var prop = arguments[idx]; @@ -107,7 +106,7 @@ Class.extend = function (props) { var hasSuperCall = fnTest.test(prop[name]); if (isFunc && override && hasSuperCall) { - desc.value = (function (name, fn) { + nonEnumerableDesc.value = (function (name, fn) { return function () { var tmp = this._super; @@ -123,10 +122,10 @@ Class.extend = function (props) { return ret; }; })(name, prop[name]); - Object.defineProperty(prototype, name, desc); + Object.defineProperty(prototype, name, nonEnumerableDesc); } else if (isFunc) { - desc.value = prop[name]; - Object.defineProperty(prototype, name, desc); + nonEnumerableDesc.value = prop[name]; + Object.defineProperty(prototype, name, nonEnumerableDesc); } else { prototype[name] = prop[name]; } @@ -160,7 +159,7 @@ cc.defineGetterSetter = function (proto, prop, getter, setter, getterName, sette getter && proto.__defineGetter__(prop, getter); setter && proto.__defineSetter__(prop, setter); } else if (Object.defineProperty) { - var desc = { enumerable: false, configurable: true }; + var desc = { configurable: true }; // enumerable is false by default getter && (desc.get = getter); setter && (desc.set = setter); Object.defineProperty(proto, prop, desc); diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index d18cbe33da..471397ed53 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ + var JS = cc.js; var SgHelper = require('./scene-graph-helper'); var Destroying = require('../platform/CCObject').Flags.Destroying; @@ -712,11 +713,6 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ }, ctor: function () { - // dont reset _id when destroyed - Object.defineProperty(this, '_id', { - value: '', - enumerable: false - }); /** * Current scene graph node for this node. @@ -761,6 +757,23 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ this._sgNode = null; }, + _destruct: function () { + // The same as super but dont reset _id when destroyed + for (var key in this) { + if (this.hasOwnProperty(key) && key !== '_id') { + switch (typeof this[key]) { + case 'string': + this[key] = ''; + break; + case 'object': + case 'function': + this[key] = null; + break; + } + } + } + }, + // ABSTRACT INTERFACES // called when the node's parent changed diff --git a/cocos2d/core/value-types/CCEnum.js b/cocos2d/core/value-types/CCEnum.js index c377b6e2ef..8b4bf3642f 100644 --- a/cocos2d/core/value-types/CCEnum.js +++ b/cocos2d/core/value-types/CCEnum.js @@ -67,7 +67,7 @@ cc.Enum = function (obj) { } Object.defineProperty(enumType, reverseKey, { value: key, - enumerable: false + // enumerable is false by default }); } }