object functions javascript 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: object inside object javascript
var obj = {
prop1: 5,
obj2: {
prop1: [3, 6, 3],
prop2: 74,
prop3: {
str: "Hello World"
}
}
};
console.log(obj.obj2.prop3.str);
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: javascript object
let names = {
name1: 'What ever you want to put'
}