Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
¿Puede pensar en una forma de llamar a hasOwnProperty en un objeto que tiene su propia propiedad con ese nombre?

este es mi codigo

 let map = {one: true, two: true, hasOwnProperty: true}; console.log(map.hasOwnProperty("one"));//error map.hasOwnProperty is not a function

El código anterior debería devolver verdadero . ¿Cómo lograr esto?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Hay un par de maneras:

  1. Object.prototype.hasOwnProperty.call(map, "one") .

  2. El nuevo Object.hasOwn(map, "one") , si sus entornos de destino lo admiten (si no es así, se rellena fácilmente), que se agregó precisamente debido a este problema.

Ejemplo:

 let map = {one: true, two: true, hasOwnProperty: true}; console.log( "Object.prototype.hasOwnProperty.call:", Object.prototype.hasOwnProperty.call(map, "one") ); if (Object.hasOwn) { console.log("Object.hasOwn:", Object.hasOwn(map, "one")); } else { console.log("Object.hasOwn not supported in this environment yet"); }

También hay algunos métodos en Reflect que son útiles para estas cosas, como Reflect.getOwnPropertyDescriptor ( Object tiene un método similar), que devolvería un objeto que tenía value: true (porque one es una propiedad de datos, no un accesor propiedad).


Tenga en cuenta que, desafortunadamente, incluso estos métodos estáticos pueden reemplazarse, por lo que en algún momento solo debe confiar en que el entorno no está configurado activamente para mentirle. :-) Por lo que vale, aquí hay un ejemplo de un entorno comprometido:

 let map = {one: true, two: true, hasOwnProperty: true}; console.log("Setting up the lies"); const originalHasOwnProperty = Object.prototype.hasOwnProperty; const originalHasOwn = Object.hasOwn; Object.prototype.hasOwnProperty = function(propName) { if (this === map && propName === "one") { return false; // lie } return originalHasOwnProperty.call(this, propName); }; Object.hasOwn = function(obj, propName) { if (obj === map && propName === "one") { return false; // lie } return originalHasOwn.call(Object, obj, propName); }; console.log( "Object.prototype.hasOwnProperty.call:", Object.prototype.hasOwnProperty.call(map, "one") ); if (Object.hasOwn) { console.log("Object.hasOwn:", Object.hasOwn(map, "one")); } else { console.log("Object.hasOwn not supported in this environment yet"); }

Detectar modificaciones de los métodos originales puede ser complejo, por lo que si decide intentarlo, busque soluciones bien investigadas con citas sólidas. Normalmente, sin embargo, es una exageración. Lo anoto aquí solo para completar.

about 4 years ago · Juan Pablo Isaza Report

0

Puedes usar apply

 const map = {one: true, two: true, hasOwnProperty: true}; console.log(Object.hasOwnProperty.apply(map,[ "one"]));

o call

 const map = {one: true, two: true, hasOwnProperty: true}; console.log(Object.hasOwnProperty.call(map,"one"));

o bind

 const map = {one: true, two: true, hasOwnProperty: true}; console.log(Object.hasOwnProperty.bind(map)('one'));
about 4 years ago · Juan Pablo Isaza Report

0

Una sugerencia : hasOwnProperty es realmente un mal nombre de propiedad.

Pero aún así, si quieres trabajar con él. Hay algunas maneras de lograr esto :

  • Mediante el uso de Object.hasOwn()

     let map = {one: true, two: true, hasOwnProperty: true}; console.log(Object.hasOwn(map, "one")); // true

  • Mediante el uso de Object.prototype.hasOwnProperty.call()

     let map = {one: true, two: true, hasOwnProperty: true}; console.log(Object.prototype.hasOwnProperty.call(map, "one")); // true

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!