Objects js Flashcards

1
Q

What is an object?

A

In programming, an object is a fundamental concept used to represent a specific instance of a class or a data structure. It is a composite data type that contains properties (also known as attributes or variables) and methods (functions associated with the object) that define its behavior and characteristics.

An object can be thought of as a real-world entity or a digital representation of something, such as a person, a car, a bank account, etc. It encapsulates both data and behavior, allowing you to interact with and manipulate its properties and invoke its methods.

Objects are commonly used in object-oriented programming (OOP) languages like Java, Python, and JavaScript. They serve as the building blocks for creating complex programs by organizing data and behavior into reusable and modular units.

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

what is an instance?

A

an instance refers to a specific occurrence or realization of a class. It is an individual object created from a class, with its own unique set of property values and state.

This implies that an instance is a concrete manifestation or actualization of the class. It represents a specific object with its own unique properties and behaviors, following the structure and behavior defined by the class.

In simpler terms, when you create an instance of a class, you are creating a distinct object that belongs to that class, with its own set of properties and behaviors. It is like creating a real, tangible object based on a blueprint (class) that defines its characteristics and actions. Each instance is separate from others, even if they are created from the same class, and can be manipulated and accessed independently.

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

What is a class?

A

A class is a blueprint or template for creating objects in object-oriented programming (OOP). It defines the properties and behaviors that an object of that class will have. In other words, a class provides a set of instructions for creating objects with a specific structure and functionality.

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

How do we build objects in js?

A

Objects are similar to arrays, except that instead of using indexes to access and modify their data, you access the data in objects through what are called properties.

Objects are useful for storing data in a structured way, and can represent real world objects, like a cat.

Here’s a sample cat object:

const cat = {
“name”: “Whiskers”,
“legs”: 4,
“tails”: 1,
“enemies”: [“Water”, “Dogs”]
};

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

when do we use . notation to access values in objects?

A

Dot notation is what you use when you know the name of the property you’re trying to access ahead of time. obj.propterty

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

What is the second way to access the properties of an object?

A

The second way to access the properties of an object is bracket notation ([]). If the property of the object you are trying to access has a space in its name, you will need to use bracket notation.

Note that property names with spaces in them must be in quotes (single or double).

const myObj = {
“Space Name”: “Kirk”,
“More Space”: “Spock”,
“NoSpace”: “USS Enterprise”
};

myObj[“Space Name”];
myObj[‘More Space’];
myObj[“NoSpace”];

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

How do we update values in objects?

A

After you’ve created a JavaScript object, you can update its properties at any time just like you would update any other variable. You can use either dot or bracket notation to update.

ourDog.name = “Happy

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

How can we add new properties to Js objects?

A

You can add new properties to existing JavaScript objects the same way you would modify them.

Here’s how we would add a bark property to ourDog:

ourDog.bark = “bow-wow”;
or

ourDog[“bark”] = “bow-wow”;
Now when we evaluate ourDog.bark, we’ll get his bark, bow-wow.

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

How do we delete properties of Js objects?

A

We can also delete properties from objects like this:

delete ourDog.bark;

const ourDog = {
“name”: “Camper”,
“legs”: 4,
“tails”: 1,
“friends”: [“everything!”],
“bark”: “bow-wow”
};

delete ourDog.bark;

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

How to check if a property on a given object exists?

A

To check if a property on a given object exists or not, you can use the .hasOwnProperty() method. someObject.hasOwnProperty(someProperty) returns true or false depending on if the property is found on the object or not.

Example

function checkForProperty(object, property) {
return object.hasOwnProperty(property);
}

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

How do we change values in const declarations?

A

We can not user s = [1,2,4], we need to point out the index and change it s[0] = 5;

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

How do we prevent mutations on objects?

A

As seen in the previous challenge, const declaration alone doesn’t really protect your data from mutation. To ensure your data doesn’t change, JavaScript provides a function Object.freeze to prevent data mutation.

Any attempt at changing the object will be rejected, with an error thrown if the script is running in strict mode.

let obj = {
name:”FreeCodeCamp”,
review:”Awesome”
};
Object.freeze(obj);
obj.review = “bad”;
obj.newProp = “Test”;
console.log(obj);

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

Could you explain the Rest parameter?

A

With the rest parameter, you can create functions that take a variable number of arguments. These arguments are stored in an array that can be accessed later from inside the function.

function howMany(…args) {
return “You have passed “ + args.length + “ arguments.”;
}
console.log(howMany(0, 1, 2));
console.log(howMany(“string”, null, [1, 2, 3], { }));

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

Explain spread operator

A

ES6 introduces the spread operator, which allows us to expand arrays and other expressions in places where multiple parameters or elements are expected.

const spreaded = […arr];

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