Methods are actions that can be performed on objects.
Methods are functions stored as property values.
In JavaScript, objects are collections of related properties (data) and methods (functions that perform actions).
name or age). They are stored as key-value pairs.- Methods are functions stored as property values that define the behaviors or actions an object can perform (e.g., a person
walk()ortalk()). - Nearly all objects in JavaScript inherit properties and methods from
Object.prototype.
Object constructor that allow you to work with objects. These are static methods used directly on the Object constructor itself, rather than an object instance.Object.keys(obj): Returns an array of an object's own enumerable string property names.Object.values(obj): Returns an array containing the values of an object's own enumerable string properties.Object.entries(obj): Returns a nested array of an object's own enumerable string key-value pairs.Object.assign(target, source): Copies all enumerable own properties from one or more source objects to a target object.Object.create(proto): Creates a new object with the specified prototype object and properties.Object.freeze(obj): Prevents any extensions of an object and makes existing properties non-writable.Object.seal(obj): Prevents new properties from being added, but allows modification of existing properties.
const person = {
firstName: "John",lastName: "Doe",
age: 50,
fullName: function() {
return this.firstName + " " + this.lastName;
}
};
const objectMethod =function(){
console.log("name:"+this.name); console.log("RegNo:"+this.regNo); console.log("NetWOrth:"+this.netWorth);
}
firstName: "John",
lastName: "Doe",
id: 5566,
getId: function() {
return this.id;
}
};
let number = person.getId();
In the example above, this refers to the person object.
this.id means the id property of the person object.
person.name = function () {
return (this.firstName + " " + this.lastName).toUpperCase();
};
Nested Objects
myObj = {
name:"John",
age:30,
myCars: {
car1:"Ford",
car2:"BMW",
car3:"Fiat"
}
}
console.log(myObj.myCars.car2)
JavaScript Destructuring
let {firstName, lastName} = person;
It can also unpack arrays and any other iterables:
let [firstName, lastName] = person;
// Create an Object
const person = {
firstName: "John",
lastName: "Doe",
age: 50
};
// Destructuring
let {firstName, lastName} = person;
The order of the properties does not matter:
// Destructuring
let {lastName, firstName} = person;
JavaScript Object Prototypes
All JavaScript objects inherit properties and methods from a prototype.
In the previous chapter we learned how to use an object constructor:
Example
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
}
const myFather = new Person("John", "Doe", 50, "blue");
const myMother = new Person("Sally", "Rally", 48, "green");We also learned that you cannot add a new property to an existing object constructor:
Example
Person.nationality = "English";To add a new property to a constructor, you must add it to the constructor function:
Example
function Person(first, last, age, eyecolor) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.eyeColor = eyecolor;
this.nationality = "English";
}