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 --- .../reference/operators/new.target/index.html | 139 +++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 files/es/web/javascript/reference/operators/new.target/index.html (limited to 'files/es/web/javascript/reference/operators/new.target') diff --git a/files/es/web/javascript/reference/operators/new.target/index.html b/files/es/web/javascript/reference/operators/new.target/index.html new file mode 100644 index 0000000000..0faa0f0878 --- /dev/null +++ b/files/es/web/javascript/reference/operators/new.target/index.html @@ -0,0 +1,139 @@ +--- +title: new.target +slug: Web/JavaScript/Referencia/Operadores/new.target +tags: + - Clases + - ECMAScript6 + - JavaScript + - Referencia +translation_of: Web/JavaScript/Reference/Operators/new.target +--- +
{{JSSidebar("Operators")}}
+ +

La propiedad new.target te permite detectar si una función o constructor fue llamado usando el operador new. En constructores y funciones instanciadas con el operador new, new.target devuelve una referencia al constructor o función. En llamadas a funciones normales, new.target es {{jsxref("undefined")}}.

+ +

Sintaxis

+ +
new.target
+ +

Descripción

+ +

La sintaxis new.target consiste en el keyword "new", un punto, y el nombre de propiedad "target". Normalmente "new." sirve como contexto para el acceso a la propiedad, pero aquí, "new." no es realmente un objeto. En llamadas a constructores, sin embargo, new.target hace referencia al constructor invocado por new por lo que "new." se convierte en un contexto virtual.

+ +

La propiedad new.target es una meta propiedad que está disponible para todas las funciones. En funciones flecha, new.target se refiere al new.target de la función que la contiene.

+ +

Ejemplos

+ +

new.target en llamadas a funciones

+ +

En llamadas a funciones normales (en contraposición a llamadas a constructores), new.target es {{jsxref("undefined")}}. Esto te permite detectar si la función fue llamada con new como constructor.

+ +
function Foo() {
+  if (!new.target) throw 'Foo() debe ser llamado con new';
+  console.log('Foo instanciado con new');
+}
+
+Foo(); // Lanza "Foo() debe ser llamado con new"
+new Foo(); // escribe en el log "Foo instanciado con new"
+
+ +

new.target en constructores

+ +

En constructores de clase, new.target hace referencia al constructor que fue directamente invocado por new. Este también es el caso si el constructor está en una clase padre y fue delegado desdes el constructor hijo.

+ +
class A {
+  constructor() {
+    console.log(new.target.name);
+  }
+}
+
+class B extends A { constructor() { super(); } }
+
+var a = new A(); // escribe en el log "A"
+var b = new B(); // escribe en el log "B"
+
+ +

Especificaciones

+ + + + + + + + + + + + + + + + + + + +
EspecificaciónEstatusComentario
{{SpecName('ES2015', '#sec-built-in-function-objects', 'Built-in Function Objects')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-built-in-function-objects', 'Built-in Function Objects')}}{{Spec2('ESDraft')}} 
+ +

Compatibilidad en navegadores

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(46.0)}}{{CompatGeckoDesktop(41)}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatChrome(46.0)}}{{CompatGeckoMobile(41)}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatChrome(46.0)}}
+
+ +

Ver también

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