1const user = {
2 name: "Sicrano",
3 age: 14
4}
5
6user.hasOwnProperty('name'); // Retorna true
7user.hasOwnProperty('age'); // Retorna true
8user.hasOwnProperty('gender'); // Retorna false
9user.hasOwnProperty('address'); // Retorna false
1for (var name in object) {
2 if (object.hasOwnProperty(name)) {
3 // do something with name
4 }
5}
6
7// OR
8
9const hero = {
10 name: 'Batman'
11};
12
13hero.hasOwnProperty('name'); // => true
14hero.hasOwnProperty('realName'); // => false
1const hero = {
2 name: 'Batman'
3};
4
5hero.hasOwnProperty('name'); // => true
6hero.hasOwnProperty('realName'); // => false
1var person = {'first_name': 'bill','age':20};
2
3if ( person.hasOwnProperty('first_name') ) {
4 //person has a first_name property
5}
6
1// the coolest way
2const obj = {
3 weather: 'Sunny;
4}
5
6if('weather' in obj) {
7 // do something
8}
9
10