From a55b575e8089ee6cab7c5c262a7e6db55d0e34d6 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:46:50 +0100 Subject: unslug es: move --- .../object/defineproperties/index.html | 194 +++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 files/es/web/javascript/reference/global_objects/object/defineproperties/index.html (limited to 'files/es/web/javascript/reference/global_objects/object/defineproperties') diff --git a/files/es/web/javascript/reference/global_objects/object/defineproperties/index.html b/files/es/web/javascript/reference/global_objects/object/defineproperties/index.html new file mode 100644 index 0000000000..3002dd200d --- /dev/null +++ b/files/es/web/javascript/reference/global_objects/object/defineproperties/index.html @@ -0,0 +1,194 @@ +--- +title: Object.defineProperties() +slug: Web/JavaScript/Referencia/Objetos_globales/Object/defineProperties +tags: + - ECMAScript5 + - JavaScript + - JavaScript 1.8.5 + - Método(2) + - Objeto +translation_of: Web/JavaScript/Reference/Global_Objects/Object/defineProperties +--- +
{{JSRef("Objetos_globales", "Object")}}
+ +

Sumario

+ +

El metodo Object.defineProperties() define nuevas o modifica propiedades existentes directamente en el objeto, retornando el objeto.

+ +

Sintáxis

+ +
Object.defineProperties(obj, propiedades)
+ +

Parámetros

+ +
+
obj
+
El objeto sobre el cual se crearán o modificaran sus propiedades.
+
propiedades
+
Un objeto cuyas propiedades enumerables propias consituyen descriptores para las propiedades a ser definidas o modificadas.
+
+ +

Descripción

+ +

Object.defineProperties, en escencia, define todas las propiedades correspondientes a las propiedades propias con capacidad de enumeración de props en el objeto objrops.

+ +

Ejemplo

+ +
Object.defineProperties(obj, {
+  "property1": {
+    value: true,
+    writable: true
+  },
+  "property2": {
+    value: "Hello",
+    writable: false
+  }
+  // etc. etc.
+});
+ +

Polyfill

+ +

Asumiendo una ejecución pristina del entorno con todos los nombres y propiedades referidas a sus valores iniciales, Object.defineProperties es casi completamente equivalente (note el comentario en isCallable) a la siguiente reimplementación de JavaScript:

+ +
function defineProperties(obj, properties) {
+  function convertToDescriptor(desc) {
+    function hasProperty(obj, prop) {
+      return Object.prototype.hasOwnProperty.call(obj, prop);
+    }
+
+    function isCallable(v) {
+      // NB: modify as necessary if other values than functions are callable.
+      return typeof v === "function";
+    }
+
+    if (typeof desc !== "object" || desc === null)
+      throw new TypeError("bad desc");
+
+    var d = {};
+
+    if (hasProperty(desc, "enumerable"))
+      d.enumerable = !!obj.enumerable;
+    if (hasProperty(desc, "configurable"))
+      d.configurable = !!obj.configurable;
+    if (hasProperty(desc, "value"))
+      d.value = obj.value;
+    if (hasProperty(desc, "writable"))
+      d.writable = !!desc.writable;
+    if ( hasProperty(desc, "get") ) {
+      var g = desc.get;
+
+      if (!isCallable(g) && g !== "undefined")
+        throw new TypeError("bad get");
+      d.get = g;
+    }
+    if ( hasProperty(desc, "set") ) {
+      var s = desc.set;
+      if (!isCallable(s) && s !== "undefined")
+        throw new TypeError("bad set");
+      d.set = s;
+    }
+
+    if (("get" in d || "set" in d) && ("value" in d || "writable" in d))
+      throw new TypeError("identity-confused descriptor");
+
+    return d;
+  }
+
+  if (typeof obj !== "object" || obj === null)
+    throw new TypeError("bad obj");
+
+  properties = Object(properties);
+
+  var keys = Object.keys(properties);
+  var descs = [];
+
+  for (var i = 0; i < keys.length; i++)
+    descs.push([keys[i], convertToDescriptor(properties[keys[i]])]);
+
+  for (var i = 0; i < descs.length; i++)
+    Object.defineProperty(obj, descs[i][0], descs[i][1]);
+
+  return obj;
+}
+ +

Especificaciones

+ + + + + + + + + + + + + + + + + + + +
EspecificaciónEstadoComentario
{{SpecName('ES5.1', '#sec-15.2.3.7', 'Object.defineProperties')}}{{Spec2('ES5.1')}}Definición inicial. Implementada en JavaScript 1.8.5
{{SpecName('ES6', '#sec-object.defineproperties', 'Object.defineProperties')}}{{Spec2('ES6')}} 
+ +

Compatibilidad de navegadores

+ +

Basado en Kangax's compat tables.

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
CaracteristicaFirefox (Gecko)ChromeInternet ExplorerOperaSafari
Soporte básico{{CompatGeckoDesktop("2")}}5 (previous versions untested)911.605
+
+ +
+ + + + + + + + + + + + + + + + + + + +
CaracteristicaFirefox Mobile (Gecko)AndroidIE MobileOpera MobileSafari Mobile
Soporte básico{{CompatGeckoMobile("2")}}{{CompatVersionUnknown}}{{CompatUnknown}}11.50{{CompatVersionUnknown}}
+
+ +

Ver también

+ + -- cgit v1.2.3-54-g00ecf