object var javascript code example
Example 1: object object javascript
person = {
'name':'john smith'
'age':41
};
console.log(person);
console.log(JSON.stringify(person));
Example 2: javascript object
var person = {
first_name : "Marty",
last_name : "Mcfly",
born : 1968,
died : 1933,
lovers: ["Jennifer Parker","Baines McFly"]
};
Example 3: creating an object javascript
let obj = {
name:"value",
num: 123,
foo: function(){}
}
Example 4: 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 5: objects javascript
function Dog() {
this.name = "Bailey";
this.colour = "Golden";
this.breed = "Golden Retriever";
this.age = 8;
}
Example 6: 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()