This commit is contained in:
Spencer Alger 2015-04-30 04:36:32 -07:00
parent e2f8f8e94f
commit b7e35e6737
2 changed files with 41 additions and 7 deletions

View file

@ -9,6 +9,22 @@ define(function (require) {
this.prototype = prototype || Object.prototype;
}
ObjDefine.REDEFINE_SUPPORTED = (function () {
var a = Object.create(Object.prototype, {
prop: {
configurable: true,
value: 1
}
});
Object.defineProperty(a, 'prop', {
configurable: true,
value: 2
});
return a.prop === 2;
}());
/**
* normal value, writable and exported in JSON
*
@ -51,7 +67,20 @@ define(function (require) {
* @return {object} - created object
*/
ObjDefine.prototype.create = function () {
return this.obj = Object.create(this.prototype, this.descs);
var self = this;
self.obj = Object.create(this.prototype, self.descs);
if (!Object.REDEFINE_SUPPORTED && !self.prototype.toJSON) {
self.obj.toJSON = function () {
return _.transform(self.obj, function (json, val, key) {
var desc = self.descs[key];
if (desc && desc.enumerable && val == null) return;
json[key] = val;
}, {});
};
}
return self.obj;
};
@ -68,7 +97,7 @@ define(function (require) {
var self = this;
var exists = val != null;
if (exported) {
if (exported && ObjDefine.REDEFINE_SUPPORTED) {
return {
enumerable: exists,
configurable: true,
@ -85,6 +114,15 @@ define(function (require) {
};
}
if (exported && !ObjDefine.REDEFINE_SUPPORTED) {
return {
enumerable: true,
configurable: true,
writable: changeable,
value: val
};
}
return {
enumerable: false,
writable: changeable,

View file

@ -35,11 +35,6 @@ define(function (require) {
});
it('switched to exporting if a value is written', function () {
if (/PhantomJS/.test(window.navigator.userAgent)) {
// disabled in phantom until https://github.com/ariya/phantomjs/issues/11856 is resolved
return;
}
var def = new ObjDefine();
def.writ('name');
@ -63,6 +58,7 @@ define(function (require) {
obj.name = null;
expect(flatten(obj)).to.not.have.property('name');
});
});
describe('#fact', function () {