“HaswnProperty” Código de respuesta

JavaScript HasownProperty

var person = {	
  	"first_name": "Bob",
	"last_name": "Dylan"
};
person.hasOwnProperty('first_name'); //returns true
person.hasOwnProperty('age'); //returns false
Grepper

JavaScript tiene objetos tiene propiedades

var person = {'first_name': 'bill','age':20};

if ( person.hasOwnProperty('first_name') ) {
    //person has a first_name property
}
Grepper

HaswnProperty

The hasOwnProperty() method returns a boolean indicating whether the object has the specified property as its own property (as opposed to inheriting it).

const object1 = {};
object1.property1 = 42;

console.log(object1.hasOwnProperty('property1'));
// expected output: true

console.log(object1.hasOwnProperty('toString'));
// expected output: false
Thankful Termite

Qué confiable es JS HaswnProperty

o = new Object();
o.propOne = null;
o.hasOwnProperty('propOne');   // returns true
o.propTwo = undefined;  
o.hasOwnProperty('propTwo');   // returns true
Thankful Tamarin

HaswnProperty

// JavaScript does not protect hasOwnProperty method
var foo = {
    // overriding foo's default hasOwnProperty method
    hasOwnProperty: function() {
        return false;
    },
    bar: 'data'
};
foo.hasOwnProperty('bar'); // false always

// Hence, to prevent this, use Object.prototype.hasOwnProperty as follows-
Object.prototype.hasOwnProperty.call(foo, 'bar'); // true
D@RK$T@R

HaswnProperty

//
//2021 - Object.hasOwn as a replacement for Object.hasOwnProperty()

//As other answers indicated, hasOwnProperty will check for an object own properties in contrast to in which will also check for inherited properties.

//There is a new alternative method called Object.hasOwn() and is intended to be a replacement for Object.hasOwnProperty()**

const person = { name: 'dan' };

console.log(Object.hasOwn(person, 'name'));// true
console.log(Object.hasOwn(person, 'age'));// false

const person2 = Object.create({gender: 'male'});

console.log(Object.hasOwn(person2, 'gender'));// false
 Run code snippet
Grieving Gharial

Respuestas similares a “HaswnProperty”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código