| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty | [Back] [Original] |
Get to know MDN better
Esta pgina ha sido traducida del ingls por la comunidad. Aprende ms y nete a la comunidad de MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since julio de 2015.
El mtodo hasOwnProperty() devuelve un booleano indicando si el objeto tiene la propiedad especificada.
obj.hasOwnProperty(prop)
propEl nombre de la propiedad a buscar.
Todo objeto descendiente de Object hereda el mtodo hasOwnProperty. Este mtodo puede ser usando para determinar si un objeto tiene la propiedad especificada como una propiedad directa de ese objeto; a diferencia del operador in, este mtodo no verifica la cadena prototipo del objeto.
hasOwnProperty para comprobar la existencia de una propiedadEl siguiente ejemplo determina si el objeto o contiene una propiedad llamada prop:
o = new Object();
o.prop = "exists";
function changeO() {
o.newprop = o.prop;
delete o.prop;
}
o.hasOwnProperty("prop"); // returns true
changeO();
o.hasOwnProperty("prop"); // returns false
El siguiente ejemplo diferencia entre propiedades directas y propiedades heredadas a travs de la cadena prototype:
o = new Object();
o.prop = "exists";
o.hasOwnProperty("prop"); // returns true
o.hasOwnProperty("toString"); // returns false
o.hasOwnProperty("hasOwnProperty"); // returns false
El siguiente ejemplo muestra como iterar sobre las propiedades de un objeto sin ejecutar sobre propiedades heredadas. Observe que el bucle for..in ya est no solo iterando elementos enumerables, por consiguiente uno no debera asumir que basado en la falta de propiedades no numerales mostrando en el bucle que hasOwnProperty por si misma no est solo es estrictamente para iterar elementos numerados (como con Object.getOwnPropertyNames()).
var buz = {
fog: "stack",
};
for (var name in buz) {
if (buz.hasOwnProperty(name)) {
alert("this is fog (" + name + ") for sure. Value: " + buz[name]);
} else {
alert(name); // toString or something else
}
}
hasOwnProperty como una propiedadJavaScript no protege el nombre de la propiedad hasOwnProperty; en consecuencia, si existe la posibilidad de que un objeto pudiera tener la propiedad con ese nombre, es necesario usar un externo hasOwnProperty para obtener los correctos resultados:
var foo = {
hasOwnProperty: function () {
return false;
},
bar: "Here be dragons",
};
foo.hasOwnProperty("bar"); // always returns false
// Use another Object's hasOwnProperty and call it with 'this' set to foo
({}).hasOwnProperty.call(foo, "bar"); // true
// It's also possible to use the hasOwnProperty property from the Object property for this purpose
Object.prototype.hasOwnProperty.call(foo, "bar"); // true
Observe que en el ltimo caso no han habido nuevos objetos creados.
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-object.prototype.hasownproperty |
Object.getOwnPropertyNames()inThis page was last modified on 19 jun 2025 by MDN contributors.
ObjectObject.assign()Object.create()Object.defineProperties()Object.defineProperty()Object.entries()Object.freeze()Object.fromEntries()Object.getOwnPropertyDescriptor()Object.getOwnPropertyDescriptors()Object.getOwnPropertyNames()Object.getOwnPropertySymbols()Object.getPrototypeOf()groupBy()Object.hasOwn()Object.is()Object.isExtensible()Object.isFrozen()Object.isSealed()Object.keys()Object.preventExtensions()Object.seal()Object.setPrototypeOf()Object.values()Object/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |