diff --git a/cocos2d/core/platform/CCObject.js b/cocos2d/core/platform/CCObject.js index 725b21aed4..f028a99f71 100644 --- a/cocos2d/core/platform/CCObject.js +++ b/cocos2d/core/platform/CCObject.js @@ -220,13 +220,27 @@ JS.getset(prototype, 'name', ); /** - * !#en Indicates whether the object is not yet destroyed. - * !#zh 表示该对象是否可用(被销毁后将不可用)。 + * !#en + * Indicates whether the object is not yet destroyed. (It will not be available after being destroyed)
+ * When an object's `destroy` is called, it is actually destroyed after the end of this frame. + * So `isValid` will return false from the next frame, while `isValid` in the current frame will still be true. + * If you want to determine whether the current frame has called `destroy`, use `cc.isValid(obj, true)`, + * but this is often caused by a particular logical requirements, which is not normally required. + * + * !#zh + * 表示该对象是否可用(被 destroy 后将不可用)。
+ * 当一个对象的 `destroy` 调用以后,会在这一帧结束后才真正销毁。因此从下一帧开始 `isValid` 就会返回 false,而当前帧内 `isValid` 仍然会是 true。如果希望判断当前帧是否调用过 `destroy`,请使用 `cc.isValid(obj, true)`,不过这往往是特殊的业务需求引起的,通常情况下不需要这样。 + * * @property {Boolean} isValid * @default true * @readOnly * @example - * cc.log(obj.isValid); + * var node = new cc.Node(); + * cc.log(node.isValid); // true + * node.destroy(); + * cc.log(node.isValid); // true, still valid in this frame + * // after a frame... + * cc.log(node.isValid); // false, destroyed in the end of last frame */ JS.get(prototype, 'isValid', function () { return !(this._objFlags & Destroyed); @@ -244,13 +258,12 @@ var deferredDestroyTimer = null; * !#en * Destroy this Object, and release all its own references to other objects.
* Actual object destruction will delayed until before rendering. - *
- * After destroy, this CCObject is not usable any more. + * From the next frame, this CCObject is not usable any more. * You can use cc.isValid(obj) to check whether the object is destroyed before accessing it. * !#zh * 销毁该对象,并释放所有它对其它对象的引用。
- * 销毁后,CCObject 不再可用。您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。 - * 实际销毁操作会延迟到当前帧渲染前执行。 + * 实际销毁操作会延迟到当前帧渲染前执行。从下一帧开始,CCObject 将不再可用。 + * 您可以在访问对象之前使用 cc.isValid(obj) 来检查对象是否已被销毁。 * @method destroy * @return {Boolean} whether it is the first time the destroy being called * @example @@ -455,17 +468,32 @@ prototype._deserialize = null; */ /** - * !#en Checks whether the object is non-nil and not yet destroyed. - * !#zh 检查该对象是否不为 null 并且尚未销毁。 + * !#en + * Checks whether the object is non-nil and not yet destroyed.
+ * When an object's `destroy` is called, it is actually destroyed after the end of this frame. + * So `isValid` will return false from the next frame, while `isValid` in the current frame will still be true. + * If you want to determine whether the current frame has called `destroy`, use `cc.isValid(obj, true)`, + * but this is often caused by a particular logical requirements, which is not normally required. + * + * !#zh + * 检查该对象是否不为 null 并且尚未销毁。
+ * 当一个对象的 `destroy` 调用以后,会在这一帧结束后才真正销毁。因此从下一帧开始 `isValid` 就会返回 false,而当前帧内 `isValid` 仍然会是 true。如果希望判断当前帧是否调用过 `destroy`,请使用 `cc.isValid(obj, true)`,不过这往往是特殊的业务需求引起的,通常情况下不需要这样。 + * * @method isValid * @param {any} value + * @param {Boolean} [strictMode=false] - If true, Object called destroy() in this frame will also treated as invalid. * @return {Boolean} whether is valid * @example - * cc.log(cc.isValid(target)); + * var node = new cc.Node(); + * cc.log(cc.isValid(node)); // true + * node.destroy(); + * cc.log(cc.isValid(node)); // true, still valid in this frame + * // after a frame... + * cc.log(cc.isValid(node)); // false, destroyed in the end of last frame */ -cc.isValid = function (value) { +cc.isValid = function (value, strictMode) { if (typeof value === 'object') { - return !!value && !(value._objFlags & Destroyed); + return !!value && !(value._objFlags & (strictMode ? (Destroyed | ToDestroy) : Destroyed)); } else { return typeof value !== 'undefined'; diff --git a/cocos2d/core/value-types/CCVec2.js b/cocos2d/core/value-types/CCVec2.js index 02aadcc517..6498782991 100644 --- a/cocos2d/core/value-types/CCVec2.js +++ b/cocos2d/core/value-types/CCVec2.js @@ -225,7 +225,7 @@ proto.mulSelf = function (num) { /** * !#en Multiplies by a number, and returns the new result. - * !#zh 缩放当前向量,并返回新结果。 + * !#zh 缩放向量,并返回新结果。 * @method mul * @param {number} num * @param {Vec2} [out] - optional, the receiving vector diff --git a/test/qunit/unit-es5/test-object.js b/test/qunit/unit-es5/test-object.js index 0c3194c064..4d47efafe8 100644 --- a/test/qunit/unit-es5/test-object.js +++ b/test/qunit/unit-es5/test-object.js @@ -38,6 +38,11 @@ test('cc.isValid', function () { strictEqual(cc.isValid(), false, 'undefined return false 1'); strictEqual(cc.isValid(obj), false, 'undefined return false 2'); strictEqual(cc.isValid(null), false, 'null return false'); + + var obj2 = new CCObject(); + obj2.destroy(); + strictEqual(cc.isValid(obj2), true, 'still valid in this frame'); + strictEqual(cc.isValid(obj2, true), false, 'indicates whether it will be destroyed'); }); test('deferred destroy', function () {