From c0836631623b1467470dfca4c0047724e38f5d61 Mon Sep 17 00:00:00 2001 From: "kimi.zhang" Date: Wed, 2 Apr 2014 13:50:29 +0800 Subject: [PATCH] The same to Object.defineProperty. Relevant to #29 --- object.js | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/object.js b/object.js index e1e231c..661a742 100644 --- a/object.js +++ b/object.js @@ -67,6 +67,7 @@ define(function (require) { hasOwnProp = 'hasOwnProperty', doc = typeof document != 'undefined' && document, testEl = doc && doc.createElement('p'), + testFn = function () {}, undef; refObj = Object; @@ -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({}); @@ -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; } @@ -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; @@ -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) { @@ -252,7 +271,7 @@ define(function (require) { } } - function hasDefinePropertiesPrototype(fn) { + function hasDefinePropertiesFunctionPrototype(fn) { if (('defineProperties' in Object)) { try { // test it @@ -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) ? {