Objects Flashcards

Objects - 7th type of data in JS. An object is a built-in data type for storing key-value pairs. Data inside objects are unordered, and the values can be of any type.

1
Q

What is JS Objects?

A

There are 7 data types in JS. 6 of those are primitive data types:

  • string
  • number
  • boolean
  • null
  • undefined
  • symbol

7th- Objects-> we can you Objects to model real- world things like a basketball.

JS Objects are containers storing related data and functionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Creating OBJECT literals?

A

Objects can be assigned to variables just like any other JS data types

We use curly braces { } to designate an object literal.

let spaceship = { } ; // spaceship is an empty object

We fill an object with unordered data.this data is organised into key-value pairs.
A key is like a variable name that holds a value.

   object
let spaceship = {            
'Fuel Types' : 'diesel' ,      
color: 'silver'                        
};                 
    key-value properties

The spaceship object has 2 properties: Fuel type and color. Fuel type has quotation mark because it contains a space character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Accessing Object properties with dot notation

A

1.with dot notation:

‘hello’.length // returns 5

let spaceship ={
homePlanet: 'Earth',
color: 'silver'
};

spaceship.homePlanet; // Earth
spaceship.color; //silver
| |
objects property name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Trying to access a property that doesn’t exist on that Object

A

Undefined will be returned

spaceship.favoriteIceCream; // undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Second way to access a key’s value in an Object

Bracket notation

A

To use Bracket Notation [ ] to access Objects property, we pass in the property name (key) as a string:

let spaceship = {
'Fuel Type' : 'Turbo Fuel' ,
'Active Duty' : true,
homePlanet : 'Earth',
numCrew: 5
};

spaceship [‘Active Duty’]; //true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Using variable inside the bracket to select the keys of an Object

A
let returnAnyProp = ( objectName, propName) =>
objectName [propName];

returnAnyProp (spaceship, ‘HomePlanet’); // Earth

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Property assignment of Objects

A

Objects are mutable - we can update them after we create them.
We can use dot notation or bracket notation [ ] and the assignment operator = to add new key- value pairs to an object or change existing property.

spaceship [’ Fuel Type’] = ‘vegetable oil’,
spaceship.color = ‘gold’;

  • if the property already exists on the Object, whatever value it held before will be replaced with the newly assigned value.
  • if there was no property with that name, a new property will be added to the Object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Deleting a property from an Object

A

using delete operator

const spaceship = {
' Fuel Type' : ' Turbo Fuel',
homePlanet : 'Earth',
mission: 'Explore the universe'
};

delete spaceship.mission; // removes the mission property

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Methods in Objects

A

When data stored on an Object is a function we call that a method

A property is what an Object has.
METHOD is what an Object does!!!

Ex: console is a global JS Object
.log is a method on that Object

Math is a global JS Object.
.floor is a method on it.

Object methods are invoked by appending the Objects name with the dot operator followed by the method name and parenthesis:

const alienship = {
invade ( ) {
      console.log ("Hello....)
}
};

alienship.invade( ); // returns “ Hello ….

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Nested Objects

A

An Object might have another Object as a property which in turn might have a property that’s an array of even more Objects.

const spaceship = {
    telescope: {
           year built: 2018,
           model: '910I-XLT',
           focallength: 2032
},
    crew: {
       captain: {
               name: 'Sandra',
               degree: 'Computer Engineering',
               encourage team ( )
     { console.log('We got this!') }
                 }
}
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Pass by Reference

A

Objects are passed by reference. This means when we want to pass a variable assigned to an Object into a function as an argument, the computer interprets the parameter name as pointing to the space in memory holding that Object.As a result, functions which change Objects properties actually mutate the object permanently (even when the Object is assigned to a const variable).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Looping through Objects

A

Loops are programming tools that repeat a block of code until a condition is met.
we learned how to iterate through arrays using their numerical index, but key-value pairs in Objects aren’t ordered.

JS alternative solution for iterating through Objects with for….in syntax

for ( let variableName in outerObject.innerObject) {
console.log (‘${OuterObject.innerObject[variableName].propertyName}: ${OuterObject.innerObject[variableName].differenPropertyName}’)
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Key-valur pairs

A

Key-value pairs is a property -when property is a function it is known as a method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Key-valur pairs

A

Key-value pairs is a property -when property is a function it is known as a method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

An Object literal

A

An Object literal is composed of comma-separated key-value pairs surrounded by curly braces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Properties and values of a JavaScript object

A

A JavaScript object literal is enclosed with curly braces {}. Values are mapped to keys in the object with a colon (:), and the key-value pairs are separated by commas. All the keys are unique, but values are not.

Key-value pairs of an object are also referred to as properties.

const classOf2018 = {
students: 38,
year: 2018
}

17
Q

Restrictions in Naming Properties

A

JavaScript object key names must adhere to some restrictions to be valid. Key names must either be strings or valid identifier or variable names (i.e. special characters such as - are not allowed in key names that are not strings).

// Example of invalid key names
const trainSchedule = {
platform num: 10, // Invalid because of the space between words.
40 - 10 + 2: 30, // Expressions cannot be keys.
+compartment: ‘C’ // The use of a + sign is invalid unless it is enclosed in quotations.
}

18
Q

Lexical scope

A

Let’s define 2 functions, having the functioninnerFunc()is nested insideouterFunc().

function outerFunc() { // the outer scope let outerVar = ‘I am from outside!’; function innerFunc() { // the inner scope console.log(outerVar); // ‘I am from outside!’ } return innerFunc; } const inner = outerFunc(); inner();

Look at the last line of the snippetinner(): theinnerFunc()invokation happens outside ofouterFunc()scope. Still, how does JavaScript understand thatouterVarinsideinnerFunc()corresponds to the variableouterVarofouterFunc()?
The answer is due to lexical scoping.
JavaScript implements a scoping mechanism named lexical scoping (or static scoping). Lexical scoping means that the accessibility of variables is determined statically by the position of the variables within the nested function scopes:the inner function scope can access variables from the outer function scope.
A formal definition of lexical scope:

Thelexical scopeconsists of outer scopes determined statically.

In the example, the lexical scope ofinnerFunc()consists of the scope ofouterFunc().
Moreover, theinnerFunc()is aclosurebecause it captures the variableouterVarfrom the lexical scope.

19
Q

Scope

A

The scope is an important concept that manages the availability of variables. The scope is at the base closures, defines the idea of global and local variables.
If you’d like to code in JavaScript, understanding the scope of variables is a must

The scope is a policy that manages the availability of variables. A variable defined inside a scope is accessible only within that scope, but inaccessible outside.
In JavaScript, scopes are created by code blocks, functions, modules.
Whileconstandletvariables are scoped by code blocks, functions or modules,varvariables are scoped only by functions or modules.
Scopes can be nested. Inside an inner scope you can access the variables of an outer scope.
The lexical scope consists of the outer function scopes determined statically. Any function, no matter the place where being executed, can access the variables of its lexical scope (this is the concept ofclosure).

20
Q

Hoisting feature in JS

A

The hoisting feature in JS which allows access to function declaration before they are identified.

Ex. greetWorld ( ); - hoisting allowed

greetWorld ( ) to be called before function was identified

function greetWorld ( ) {
      console.log('Hello, World!');
}
20
Q

Hoisting feature in JS

A

The hoisting feature in JS which allows access to function declaration before they are identified.

Ex. greetWorld ( ); - hoisting allowed

greetWorld ( ) to be called before function was identified

function greetWorld ( ) {
      console.log('Hello, World!');
}