Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: patch property descriptors in Object.create
Browse files Browse the repository at this point in the history
Closes #24
  • Loading branch information
btford committed Jun 6, 2014
1 parent f587f17 commit 7b7258b
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,14 @@ Zone.patchMutationObserverClass = function (className) {
Zone.patchDefineProperty = function () {
var _defineProperty = Object.defineProperty;
var _getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
var _create = Object.create;

Object.defineProperty = function (obj, prop, desc) {
if (isUnconfigurable(obj, prop)) {
throw new TypeError('Cannot assign to read only property \'' + prop + '\' of ' + obj);
}
return rewriteDescriptor(obj, prop, desc);
desc = rewriteDescriptor(obj, prop, desc);
return _defineProperty(obj, prop, desc);
};

Object.defineProperties = function (obj, props) {
Expand All @@ -533,6 +535,15 @@ Zone.patchDefineProperty = function () {
return obj;
};

Object.create = function (obj, proto) {
if (typeof proto === 'object') {
Object.keys(proto).forEach(function (prop) {
proto[prop] = rewriteDescriptor(obj, prop, proto[prop]);
});
}
return _create(obj, proto);
};

Object.getOwnPropertyDescriptor = function (obj, prop) {
var desc = _getOwnPropertyDescriptor(obj, prop);
if (isUnconfigurable(obj, prop)) {
Expand All @@ -542,24 +553,23 @@ Zone.patchDefineProperty = function () {
};

Zone._redefineProperty = function (obj, prop, desc) {
return rewriteDescriptor(obj, prop, desc);
desc = rewriteDescriptor(obj, prop, desc);
return _defineProperty(obj, prop, desc);
};

function isUnconfigurable (obj, prop) {
return obj && obj.__unconfigurables && obj.__unconfigurables[prop];
}

function rewriteDescriptor (obj, prop, desc) {
desc.configurable = true;
if (!desc.configurable) {
desc.configurable = true;
if (!obj.__unconfigurables) {
_defineProperty(obj, '__unconfigurables', {
value: {}
});
_defineProperty(obj, '__unconfigurables', { writable: true, value: {} });
}
obj.__unconfigurables[prop] = true;
}
return _defineProperty(obj, prop, desc);
return desc;
}
};

Expand Down

0 comments on commit 7b7258b

Please sign in to comment.