Skip to content

Commit

Permalink
The same to Object.defineProperty. Relevant to cujojs#29
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimi-GitHub committed Apr 2, 2014
1 parent bc8ef6d commit c083663
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions object.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ define(function (require) {
hasOwnProp = 'hasOwnProperty',
doc = typeof document != 'undefined' && document,
testEl = doc && doc.createElement('p'),
testFn = function () {},
undef;

refObj = Object;
Expand Down Expand Up @@ -125,14 +126,17 @@ define(function (require) {
'object-defineproperty-dom': function () {
return doc && hasDefineProperty(testEl);
},
'object-defineproperty-function-prototype': function () {
return hasDefinePropertyFunctionPrototype(testFn);
},
'object-defineproperties-obj': function () {
return hasDefineProperties({});
},
'object-defineproperties-dom': function () {
return doc && hasDefineProperties(testEl);
},
'object-defineproperties-prototype': function () {
return hasDefinePropertiesPrototype(function () {});
'object-defineproperties-function-prototype': function () {
return hasDefinePropertiesFunctionPrototype(testFn);
},
'object-getownpropertydescriptor-obj': function () {
return hasGetOwnPropertyDescriptor({});
Expand Down Expand Up @@ -199,7 +203,7 @@ define(function (require) {
? useNativeForDom(Object.defineProperties, defineProperties)
: defineProperties;
}
else if (!has('object-defineproperties-prototype')) {
else if (!has('object-defineproperties-function-prototype')) {
Object.defineProperties = shims.defineProperties
= defineProperties;
}
Expand All @@ -211,6 +215,10 @@ define(function (require) {
? useNativeForDom(Object.defineProperty, defineProperty)
: defineProperty;
}
else if (!has('object-defineproperty-function-prototype')) {
Object.defineProperty = shims.defineProperty
= definePropertyFunctionPrototype;
}

if (!has('object-isextensible')) {
Object.isExtensible = shims.isExtensible = isExtensible;
Expand Down Expand Up @@ -239,6 +247,17 @@ define(function (require) {
}
}

function hasDefinePropertyFunctionPrototype(fn) {
if (('defineProperty' in Object)) {
try {
// test it
Object.defineProperty(fn, { 'prototype': { value: { test: true } } })
return (new fn).test === true && fn.prototype.test === true;
}
catch (ex) { /* squelch */ }
}
}

// Note: MSDN docs say that IE8 has this function, but tests show
// that it does not! JMH
function hasDefineProperties (object) {
Expand All @@ -252,7 +271,7 @@ define(function (require) {
}
}

function hasDefinePropertiesPrototype(fn) {
function hasDefinePropertiesFunctionPrototype(fn) {
if (('defineProperties' in Object)) {
try {
// test it
Expand Down Expand Up @@ -331,6 +350,23 @@ define(function (require) {
return object;
}

function definePropertyFunctionPrototype (object, name, descriptor) {
if (name == 'prototype') {
if (object[name] && typeof(object[name]) == 'object' && descriptor && descriptor.value && typeof(descriptor.value) == 'object') {
for (var i in descriptor.value) {
object[name][i] = descriptor.value[i];
}
}
else {
object[name] = descriptor && descriptor.value;
}
}
else {
Object.defineProperty.apply(Object, arguments);
}
return object;
}

function getOwnPropertyDescriptor (object, name) {
return hasProp(object, name)
? {
Expand Down

0 comments on commit c083663

Please sign in to comment.