JS Objects Flashcards
What is JavaScript Objects?
Object is an unordered collection of key-value pairs. Each key-value pair is called a property
The key of a property can be a string. And the value of a property can be any value.
They are called properties and methods when they are inside objects
How create an object ?
let empty = {};
How create an object and use the object literal??
const person = {
name: [“Bob”, “Smith”],
age: 32,
bio() {
console.log(${this.name[0]} ${this.name[1]} is ${this.age} years old.
);
},
introduceSelf() {
console.log(Hi! I'm ${this.name[0]}.
);
},
};
How create an object with properties?
To create an object with properties, you use the key:value within the curly braces
let person = {
firstName: ‘John’,
lastName: ‘Doe’
};
How accessing properties to obj ?
To access a property of an object, you use one of two notations:
-the dot notation
- array-like notation.
The dot notation (.)
let person = {
firstName: ‘John’,
lastName: ‘Doe’
};
console.log(person.firstName);
console.log(person.lastName);
Array-like notation ( []) Bracket notation
let person = {
firstName: ‘John’,
lastName: ‘Doe’
};
console.log(person[‘firstName’]);
console.log(person[‘lastName’]);
const myDataName = “height”;
const myDataValue = “1.75m”;
person[myDataName] = myDataValue;
person.height; //1.75m
When a property name contains spaces:
let address = {
‘building no’: 3960,
street: ‘North 1st street’,
state: ‘CA’,
country: ‘USA’
};
address[‘building no’];
Modifying the value of a property
let person = {
firstName: ‘John’,
lastName: ‘Doe’
};
change the value of a property, you use the assignment operator (=)
person.firstName = ‘Jane’;
console.log(person);
{ firstName: ‘Jane’, lastName: ‘Doe’ }
Adding a new property to an object
let person = {
firstName: ‘John’,
lastName: ‘Doe’
};
person.age = 25
{ firstName: ‘John’, lastName: ‘Doe’, age: 23 }
What is objects as object properties
const person = {
name: {
first: “Bob”,
last: “Smith”,
},
};
to call properties :
person.name.first;
Deleting a property of an object
delete person.age
{ firstName: ‘John’, lastName: ‘Doe’ }
Checking if a property exists
let employee = {
firstName: ‘Peter’,
lastName: ‘Doe’,
employeeId: 1
};
console.log(‘ssn’ in employee); //false
console.log(‘employeeId’ in employee); //true
check if a property exists in an object, you use the in operator
console.log(‘ssn’ in employee);
false
console.log(‘employeeId’ in employee);
true
JavaScript objects properties?
JavaScript objects have two types of properties: data properties and accessor properties.
Pass-by-value primitives values?
What is a result?
function square(x) {
x = x * x;
return x;
}
let y = 10;
let result = square(y);
console.log(result);
console.log(y);
In JS, all function arguments are always passed by value.
Any changes that you make to the arguments inside the function do not reflect the passing variables outside of the function
console.log(result); // 100
console.log(y); // 10 – no change
Pass-by-value of reference values? What is a result?
let person = {
name: ‘John’,
age: 25,
};
function increaseAge(obj) {
obj.age += 1;
obj = { name: ‘Jane’, age: 22 };
}
increaseAge(person);
console.log(person);
JavaScript passes an object by reference because the change to the object is reflected outside the function
JavaScript passes all arguments to a function by values.
Function arguments are local variables in JavaScript.
console.log(person); //{ name: ‘John’, age: 26 }
Define methods of an object using the object literal.
JavaScript allows you to define methods of an object using the object literal syntax:
let person = {
firstName: ‘John’,
lastName: ‘Doe’,
greet() {
console.log(‘Hello, World!’);
}
};
person.greet();
How to use “this value” in methods in object?
Inside a method, the this value references the object that invokes the method:
let person = {
firstName: ‘John’,
lastName: ‘Doe’,
greet: function () {
console.log(‘Hello, World!’);
},
getFullName: function () {
return this.firstName + ‘ ‘ + this.lastName;
}
};
console.log(person.getFullName()); //’John Doe’