Fichas
objects
Fichas del conjunto (2)
- 1way to check if an object has a propertyconst user = { name: 'Olivier',}
'name' in user // true
in operator checks if the property is in the specified object or its prototype chain, so 'constructor' in user // true
hasOwnProperty method indicates if the object has the specified property as its own property.
myObject.hasOwnProperty('id')
It is recommended to use: Object.prototype.hasOwnProperty.call(myObject, 'id') instead
What is the difference and why should you use the second syntax ?
1
You can create objects without any prototype in javascript this way:
const myObject = Object.create(null). Why would you do that instead of const myObject = {}?
Because it was apparently a way to simulate a map data structure (you would still get an object but without any properties or methods inherited from the prototype) before they introduce the Map data structure.
2
u can overwrite the hasOwnProperty method (or any other method) and thus:
const user = { name: 'Olivier' }
user.hasOwnProperty = null
user.hasOwnProperty('name') // Uncaught TypeError
Object.prototype.hasOwnProperty.call(user, 'name') // true - 2destructuringvar obj ={color: "blue"}
console.log(obj.color);
***
var { color, name} = { color: "blue", name: "John", city: "NY"}
console.log(color);
Información
- Descripción
- Object Fichas sobre objects, creado por milly larcen el 16/04/2020.
- Creado por
- milly larcen
- Visitas
- 0
- Valoración