“HasownProperty ()” Ответ

JavaScript HasownProperty

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

javaScript действительно имеет свойство

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

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

HasownProperty

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

Насколько надежна JS HasownProperty

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

HasownProperty

// 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

HasownProperty ()

var x = {
    y: 10
};
console.log(x.hasOwnProperty("y")); //true
console.log(x.hasOwnProperty("z")); //false
Light Lizard

Смотреть популярные ответы по языку

Смотреть другие языки программирования