Introduction to objects & Object methods Flashcards
What is an object?
An object is a collection of properties and a property is an association between a name (or key) and a value.
Example: const person = { firstName: 'James' lastName: 'Bond' };
What is the difference between Objects and Arrays ?
The difference between objects and arrays is that in objects the order of the values does not matter when retrieving the values.
How can properties be gotten from an object?
Properties can be gotten from an object using the Dot and the Bracket notation.
Example:-
DOT NOTATION
console.log(person.firstName);
BRACKET NOTATION
console.log(person[‘firstName’]);
What is the difference between the Dot & Bracket Notation.
In the Dot notation only the real property name can be used and not a computed property name.
Example:-
USING BRACKET NOTATION
const nameKey = “Name”;
console.log(person.[‘first’ + nameKey]); // output : James.
Note: the above can’t be done using Dot Notation.
How are new properties added to objects?
DOT NOTATION
person.job = ‘Spy’;
BRACKET NOTATION
person[‘car’] = ‘Aston Martin’;
What is an Object method?
An object method is a function attached to an object.
Example:-
What is an Object method?
An object method is a function attached to an object.
Example:- const person = { firstName: 'James', lastName: 'Bond', calcAge: function(currentYear, birthYear){ return currentYear - birthYear; } };