js object function code example
Example 1: how to define function in object javascript
let example = {
calc: function (num1, num2) {
return num1 + num2;
},
};
example.calc(1,2)
Example 2: object object javascript
person = {
'name':'john smith'
'age':41
};
console.log(person);
console.log(JSON.stringify(person));
Example 3: javascript object
var person = {
first_name : "Marty",
last_name : "Mcfly",
born : 1968,
died : 1933,
lovers: ["Jennifer Parker","Baines McFly"]
};
Example 4: object methods in javascript
const testScore = {
damon: 89,
shawn: 91,
keenan: 80,
kim: 89,
};
Object.keys(testScore);
Object.values(testScore);
Object.entries(testScore);
for (let person in testScore) {...}
for(let score of Object.values(testScore)){
console.log(score)
}
Example 5: js objects
const dog = {
name: "Rusty",
breed: "unknown",
isAlive: false,
age: 7
}
dog.age;
dog["age"];
dog.breed = "mutt";
dog["age"] = 8;
Example 6: adding function to objects js
var myObj = {
myFunc: function(param){
}
}