working with objects in javascript

Working with objects in JavaScript is pretty basic compared to other programming languages. If we make an array that’s an object, a date that’s an object, we can call methods on objects, functions belong to an objects and as soon as we start touching our web pages, we’ll have several objects that represent all the different pieces of the page — it’s what’s referred to as DOM or Document Object Model.

Objects allow us to gather data and variables and functions that belong together put it in a container and give them a name.

For example, we can create some regular variables…
var playerName = “Fred”;
var playerScore = 1000;
var playerRank = 1;

But these seem like they belong together, we can probably enclose this in a larger object…

Object creation:
var player1 = {name: “Fred”, score: 1000, rank:1};
var player2 = {name: “Sam,”, score: 10000000, rank:5};

Function creation:
function playerDetails() {
console.log( + “ has a rank of: “ + this.rank + “ and a score of “ + this.score);

Associating the function with the object:
player1.logDetails = playerDetails; 
player2.logDetails = playerDetails;
// you’re not calling the function here, you’re just associating it

Call the function:

This will return:
Fred has a rank of: 1 and a score of 1000
Sam, has a rank of: 5 and a score of 10000000