Objects Flashcards

1
Q

Object

A

stores any number of values associated with string “keys”

var (name) = {

(key) : (value),
(key2) : (value2)

}

(name).(key) or (name)[“(key with spaces)”]

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

Create an object, ‘me’, with the key of “name” with your name as a string. Add to the ‘me’ object a key of “age” and set it to your age, not as a string, but a number.

A
var me = {
        name: "Randy",
        age: 25
      };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Method

A

a function that is a property of an object

(object).(key)((arg)) —> console.log

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

this

A
  • refers to the global object, window
  • to be used only as a method
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

On ‘andrew’ and ‘ryan’, set the ‘greet’ method as the ‘genericGreet’ function.

var genericGreet = function() {
 return "Hello, my name is " + this.name;
 }
var andrew = {
 name: "Andrew"
 }
var ryan = {
 name: "Ryan"
 }
A
var genericGreet = function() {
 return "Hello, my name is " + this.name;
 }
var andrew = {
 name: "Andrew",
 greet: genericGreet
 }
var ryan = {
 name: "Ryan",
 greet: genericGreet
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Call vs. Apply

A
  • call takes any # of args and puts it in context of this
  • apply strictly takes an array of args
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Set the ‘greeting’ variable, by using the ‘genericGreet’ function in the context of ‘andrew’ with the array of arguments, ‘args1’.

var genericGreet = function(name, mood) {
 name = name || "you";
 mood = mood || "good";
 return "Hello " + name + ", my name is " + this.name +
 " and I am in a " + mood + " mood!";
 }
 var andrew = {
 name: "Andrew"
 }

var args1 = [“Michael”, “awesome”, “:)”];

var greeting = genericGreet;

A

var greeting = genericGreet.apply(andrew, args1);

if “name” is false or undefined, then || = “or” operator and uses “you”

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

Prototype

A

an object that provides keys and values for another object

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

Object Oriented Programming

A

programming based around creating and using objects

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

Constructor Function

A
  • special function for creating and initializing a new object
  • always capitalize first letter
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

new

A

creates new object, passes it to constructor function, and returns it

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

Modify the ‘Car’ constructor function so that it takes an argument for its model and set its ‘model’ to the model passed in from the argument.

var carPrototype = {
model: “generic”,
currentGear: 0,
increaseGear: function() {
this.currentGear ++;
},
decreaseGear: function() {
this.currentGear–;
}
}

function Car() {

}

A
function Car(model) {
 this.model = model;
 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

After the ‘Car’ constructor function, modify the Car’s prototype to be the ‘carPrototype’.

      function Car(model) {
        this.model = model;
      }
A
function Car(model) {
        this.model = model;
        Car.prototype = carPrototype;
      }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Modify the Car’s prototype to have ‘wheels’ set to 4.

      function Car(model) {
        this.model = model;
        Car.prototype = carPrototype;
      }
A
function Car(model) {
        this.model = model;
        Car.prototype = carPrototype;
        Car.prototype.wheels = 4;
      }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the values of mittens.name and mittens.legs after this code executes?

function Cat (name) {

this.name = name; this.ears = 2;

}

var mittens = new Cat(“Sir Mittens”);

Cat.prototype.legs = 4;

A

mittens. name === “Sir Mittens”;
mittens. legs = 4;

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

How do call() and apply differ?

A

apply takes all the arguments as a single array, where call passes all arguments individually

17
Q

When a normal function is called, what is the value of “this”? (assuming the current JavaScript rules, not the upcoming version 5, or “strict mode”)

A

window

18
Q

Why do we name our construction functions with a leading capital letter?

A

it’s a convention which allows us to recognize where the “new” keyword should be used

19
Q

What does ‘this’ generally represent in a method?

A

the object to which the method belongs to

20
Q

What value would be displayed in the console for this code?

var person = {

name: “Mike”,
skills: [“html”, “css”]

}

person. name = “Bob”;
console. log(person[“name”]);

A

“Bob”

21
Q

What is a prototype?

A

it is an object that provides keys and values for another object, provided the other object does not itself define that key

22
Q

What is a disadvantage of using the bracket notation

( object[“key”] )

versus the dot notation

( object.key )?

A

the square bracket returns the object stored as the value, where dot notation instead returns a copy of the object which is the value