diff --git a/object.js b/object.js index 7e7dfb7..8d766d3 100644 --- a/object.js +++ b/object.js @@ -120,16 +120,22 @@ define(function (require) { 'object-isextensible': 'isExtensible', 'object-preventextensions': 'preventExtensions', 'object-defineproperty-obj': function () { - return hasDefineProperty({}); + return hasDefineProperty({}, 'sentinel1', { value: true }); }, 'object-defineproperty-dom': function () { - return doc && hasDefineProperty(testEl); + return doc && hasDefineProperty(testEl, 'sentinel1', { value: true }); + }, + 'object-defineproperty-function': function () { + return hasDefineProperty(function () {}, 'prototype', { value: { test: true } }); }, 'object-defineproperties-obj': function () { - return hasDefineProperties({}); + return hasDefineProperties({}, { 'sentinel2': { value: true } }); }, 'object-defineproperties-dom': function () { - return doc && hasDefineProperties(testEl); + return doc && hasDefineProperties(testEl, { 'sentinel2': { value: true } }); + }, + 'object-defineproperties-function': function () { + return hasDefineProperties(function () {}, { 'prototype': { value: { test: true } } }); }, 'object-getownpropertydescriptor-obj': function () { return hasGetOwnPropertyDescriptor({}); @@ -189,7 +195,7 @@ define(function (require) { = getOwnPropertyNames; } - if (!has('object-defineproperties-obj')) { + if (!has('object-defineproperties-obj') || !has('object-defineproperties-function')) { // check if dom has it (IE8) Object.defineProperties = shims.defineProperties = has('object-defineproperties-dom') @@ -197,7 +203,7 @@ define(function (require) { : defineProperties; } - if (!has('object-defineproperty-obj')) { + if (!has('object-defineproperty-obj') || !has('object-defineproperty-function')) { // check if dom has it (IE8) Object.defineProperty = shims.defineProperty = has('object-defineproperty-dom') @@ -221,12 +227,12 @@ define(function (require) { : getOwnPropertyDescriptor; } - function hasDefineProperty (object) { + function hasDefineProperty (object, prop, descriptor) { if (('defineProperty' in Object)) { try { // test it - Object.defineProperty(object, 'sentinel1', { value: true }) - return object['sentinel1'] === true; + Object.defineProperty(object, prop, descriptor) + return object[prop] === descriptor.value; } catch (ex) { /* squelch */ } } @@ -234,12 +240,21 @@ define(function (require) { // Note: MSDN docs say that IE8 has this function, but tests show // that it does not! JMH - function hasDefineProperties (object) { + function hasDefineProperties (object, props) { + var keys, property; + if (('defineProperties' in Object)) { try { // test it - Object.defineProperties(object, { 'sentinel2': { value: true } }) - return object['sentinel2'] === true; + Object.defineProperties(object, props); + keys = _keys(props); + + while (property = keys.pop()) { + if (object[property] !== props[property].value) { + return false; + } + } + return true; } catch (ex) { /* squelch */ } } diff --git a/test/object.html b/test/object.html index 4fcbb6f..8d3dbef 100644 --- a/test/object.html +++ b/test/object.html @@ -258,6 +258,13 @@ Object.defineProperty(test, 'foo', { value: 5 }); return test.foo == 5; }); + tester.assertTrue("Object.defineProperty should define the prototype property correctly", function () { + function fn() {}; + + Object.defineProperty(fn, 'prototype', { value: { test: true } }); + + return fn.prototype.test === true && (new fn).test === true; + }); tester.assertTrue('Object.defineProperties should add several properties', function () { var test = {}; // writable and get don't actually work, just putting them in here