object notation javascript code example
Example 1: javascript object notation
{
"first_name": "Taylor",
"last_name": "Hawkes",
"age": 31,
"address": {
"street": "954 Kazaam Lane",
"city": "Boulder",
"state": "CO",
"postalCode": "80303"
},
"emails": [
{
"type": "main",
"number": "[email protected]"
},
{
"type": "secondary",
"number": "[email protected]"
}
]
}
Example 2: funcion dentro de objeto
function rectangulo(base, altura) {
this.base = base;
this.altura = altura;
this.calcularArea = function () { return this.base * this.altura; };
}
var r1 = new rectangulo(2, 4);
console.log(r1.calcularArea());
Example 3: js create object with properties
var mycar = new Car('Eagle', 'Talon TSi', 1993);
Example 4: js create object with properties
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
Example 5: acceder a propiedades objetos
nombreObjeto.nombrePropiedad
Si el método no recibe parámetros colocamos los paréntesis también, pero sin nada dentro.
miObjeto.miMetodo()