Section 4: Objects and Functions Flashcards

1
Q

What values can hold an Object ? and how they are called?

A

A primitive that is called a property

An object that is called a property also

Functions that are called methods

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

An object have reference in memory to their peorperties and methods?

A

YES

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

How is important to think about an Object as…

(When we talk about how it´s an object in memory)

A

As sitting in memory, and then having references to other things sitting in memory that are connected to it, so it knows where its different properties and methods are, that is: primitives, objects and functions. (properties and methods)

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

How can we access those slots in memory, those properties and methods of an object?

and how are their names(of the operators)?

A

With 2 operators:

the dot operator . also called member access

the computed memeber access operator []

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

What does the computed member access operator [] ?

and help me to remember what an operator is, under the hood?

A

Remember, an operator is a function.

The computed member operator, takes two parameters and it’s associativity is left-to-right

to the left it takes the object, and inside the [] it takes the property or method

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

What does the dot . operator do?

A

It’s a fnction and when used after an object it takes two parameters, the object that you’re looking at and the name of the property.

It takes the object(on the left) and on the right the string that represents the name of the value that you are looking for

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

What is the other name of the dot . operator?

A

It´s also called Member Access

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

Why the dot . operator is called Member Access

A

Because members being members of the object, These are looking for members of the object, the pieces, the methods, and the properties

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

How can we create an object with object literals

A

using curly braces { }

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

What is inside the curly braces { } how is it treated, how many lines of code?

A

It´s treated as one line of code

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

When it gets really powerful to use object literals?

A

Because we can create objects on the fly

For example, on passing parameters to a function, as an object literal

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

Let´s pass some object as a parameter to this function, how can we imagine this.

function greet(person) {

console.log(“Hi ” + person.firstname);

}

A

We can call the greet funciton, and create an object on the fly

greet({

firstname: ‘Tony’,
lastname: ‘Garcia’

});

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

What “think” the JS engine when it sees:

greet({

firstname: ‘Tony’,
lastname: ‘Garcia’

});

A

it says:

“oh, you are calling a function, and oh, what are you gonna pass to it? Well, I just saw curly braces { } so I know you´re creating an object right here at this moment of execution and passing it, it`d be no different than passing anything else like a number or a string”

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

It´s true to say that “Wherever i want I can create an object as I go, and I can use an object wherever I use any variable”?

A

YES

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

What is a Namespace?

A

A container for variables and functions, typically to keep variables and functions with the same name separate.

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

JS have namespaces? and why?

A

NO, because of the nature of how objects works inJS, we don´t need it as a feature

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

How can we fake namespaces, for preventing collisions like this one:

var greet = “hello!”;

var greet = “hola!;

console.log(greet) // hola!

A

by creating an object that will be the container for our properties and methods and the things we want to use. The object it won’t really have any other functionality, and that’s okay

var english = {

greetings: {
basic: ‘Hello’

}

}

console.log(english.greetings.basic);

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

What is the idea of using objects as faking namespaces? and why?

A

We need to containe variables, methods, functions, properties from the global namespaces.

Because someone else might have written or even that we´ve written code in the global namespace and forgotten about.

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

What it means JSON, and what is JSON

A

Javascript object notation.

It´s just a string of data

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

In a JSON format, how properties needs to be wrapped?

A

in quotes “ “

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

JSON is a part of JS?

A

No

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

Imagine a JSON format file, with two properties, how it will look?

A

{

“firstname”: “Mary”,

“isAProgrammer”: true

}

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

How can we set a valid object literal in JS to a JSON format?

A

using the JSON.stringify() method

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

How can we pass a valid JSON string of data, to an Object in JS?

A

using JSON.parse() method

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

1) Anything that is JSON valid is also valid JS object literal syntax?
2) All object literal syntax is valid JSON?

A

1) YES
2) NO

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

What is the meaning of “first class functions”

A

Everything you can do with other types you can do with functions. Assign them to variables, pass them around, create them on the fly

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

In JS, functions are objects?

A

YES

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

Does a function object resides in memory?

A

Yes, it resides in memory

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

Does a function object, has all the features of a normal object and has some other special properties?

A

YEs

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

Can you attach properties and methods to a function? and why?

A

Yes, you can.

Because it´s just an object

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

Does function objects has some hidden special properties?

A

YES

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

What are the hidden properties of a function object?

A

the code property

and the name property (it could also be anonymous)

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

What does the code property of a function object do or have?

A

it has the actual lines of code that you´ve written.

So the code that you write gets placed into a special property of the function object.

34
Q

The code property of a function object, we can say that its like a …. of the function object

A

We can sey that its like a PROPERTY that you´re adding on to it

35
Q

What is the special thing about the “code property” of a function object?

A

It´s that is invocable, meaning that you can run the code that it´s in that property.

36
Q

What is the special thing that makes total sense in JS that happens when in a function object, you invoke the “code property” with the ()

A

It´s the execution context creation phase and execution happens.

37
Q

The code inside a function, it´s just a ….

A

The mental model of knowing that the code inside a function, it´s just a property, a “code property” like other property, that it´s invocable via (), meaning that the “code property” it´s going to be run.

38
Q

What is an expression?

A

a unit of code that results in a value, it doesn´t have to save to a variable

39
Q

What is the difference between a function statement and a function expression?

A

a function statement just do work

a function expression or any expression in JS ends up creating a value and that value doesn´t necessarily have to save inside a variable

40
Q

function expressions are hosited?

A

no, it just hoisted the variable

41
Q

What happens in the execution phase when it gets to a function statement

A

function statement: the function is already in memory, so it doen´t do anything “oh yeah, there´s a function there, it keeps going”

42
Q

What happens in the execution phase whe it gets into a function expression?

A

the statement results in an object being created, it returns an object essentially, so it´s an expression, it results in a value, the value being a new function object

43
Q

When we pass values “by value” what do we mean:

ej:

var a = 3;

var b = a;

A

We are creating a new address in memory, a new location in memory, and a copy of the value from “a” is setting in “b”

It´s a completely new value

44
Q

When we pass a value “by reference” what do we mean?

A

We are setting a reference to the value, the new variable has a reference to the same spot in memory that the original variable has

45
Q

When we talk about “by value” and “by reference”

Which one apply to whicn one

Primitives and objects types

A

Primitives, are passed by value

Objects, and function objects(a special type of object) are passed by reference

46
Q

What happens when we use the keyword “this” inside a function statement, to where “this” will point

A

It will point to the Global Object

47
Q

What happens when we use the keyword “this” inside a function expression, to where “this” will point

A

It will point to the Global Window Object

48
Q

What happens when we use the keyword “this” inside an object, to where “this” will point?

A

to the object itself, the object that is containing “this”

49
Q

What happens when we have the “this” keyword inside a function expression, inside a method of an object, to where it will point?, and why?

var c = {

name: ‘The c object’,
log: function() {
console. log(this);

var setName = function(newName) {

this.name = newName;

}

setName(“updated again”);

console.log(this); // added the name property into the Window Object

}

}

A

It will point to the Global Object

Why? The internal function setName(), when its execution context was created, the “this” keyword points to the global object.

50
Q

What can we do when we have the “this” keyword inside a function, that it´s inside a method of an object, how can we resolve the porlbme that the “this” keyword is pointing to the global object?

A

We know that an object are going to be set equal by reference, so we can create a new variable and reference it to the same location in memory as the “this” keyword

var c = {

name: ‘The c object’,
log: function() {

var self = this;

console.log(self);

var setName = function(newName) {

self.name = newName;

}

setName(“updated again”);

console.log(self); //

}

}

51
Q

Any internal function inside a method in an object, that is using the keyword “this” will be pointing to the Global Object, or to the object itself?

A

It will be poiting to the global object

52
Q

What is an array?

A

Its a collection of anything, it can holds many things

53
Q

How can we declare an array using array literals

A

var a = [1, 2, 3];

54
Q

Complete the sentence

Because JS is …… ….. we can put anything into an array

A

dynamically typed

55
Q

Function expressions are like a kind of a function literal were we are creating a function on the fly?

A

YES

56
Q

How can we trick the syntax parser to see a function statement like a function expression?

A

We have to make sure that the first thing that the syntax parser see, its not the word function, so we need to wrap the function with ( ) the grouping operator

57
Q

Why when we wrap a function statement with ( ) the grouping operator, the syntax parser doesn´t give an error?

A

Because everything that goes into the grouping operator ( ) it´s an expression. We only use ( ) with expressions.

58
Q

What “think” the syntax parser when it sees a funciton statement inside the grouping operator ( )

A

it must me an expression, it must be a function expression, the programmer its creating this function “on the fly”.

59
Q

How can we make a IIFE immediately function expression?

A

We need to wrap a function statement with the grouping operator ( ), and since a function, it´s a special type of object in JS, we can invoke the code property with (), so we must use the invoke sytanx at the end of the grouping operator

(function(name) {

return ‘Hello ‘ + name;

})();

60
Q

What is the purpose of having an IIFE?

A

To protect our code from other outside code

To prevent us to put something accidentally in the global object

61
Q

How can we pass information from our IIFE into the global object?

A

simply passing around parameters, objects pass by reference, so if we want access to the global object, we just pass the reference to the global object into our function

(function(global, name) {

var greeting = ‘Hello’,

global. greeting = “hola”,
console. log(greeting + ‘ ‘ + name);

})(window, ‘John’)); // Hello John

that´s the way to have our code, maybe some object in our function that we want to be available everywhere else in our code, we can stick it on the global object.

62
Q

Why we can return a function as a value?

A

Because functions are objects, are first class functions, a special type of object

63
Q

Que es un closure?

A

Los closures son funciones que manejan variables independientes. En otras palabras, la función definida en el closure “recuerda” el entorno en el que se ha creado.

Es crear una función dentro de otra función para que cuando cuando busque variables, estas sean independientes porque las busca en su propia referencia externa, porque cada función (la primera en ejecutarse) crea su propio contexto de ejecución.

Nos sirve para tener una “factory function” funciones creadoras de funciones.

64
Q

What is a callback function?

A

A function you give to another function, to be run when the other funciton is finished.

So the function you call(invoke) calls back, by calling the function you gave it when finished

function tellMeWhenDone(callback) {

var a = 1000;

var b = 2000;

callback(); // the callback, it runs the function we give it

}

tellMeWhenDone(function() {

console.log(‘done!’);

});

tellMeWhenDone(function() {

console.log(‘really done!’);

});

65
Q

What does the bind() method

A

returns a new function(makes a copy of the function, and sets up a new function object with the copy), and sets “this” to whatever object we want.

66
Q

what does call()

A

let me decide what “this” will be, and i can pass arguments to it

call() doesn´t create a new function, it works as if we invoke it

67
Q

what does apply()

A

executes a function, deciding what “this” should point to, the arguments must be passed with an array list

68
Q

What method can we use for doing “Function borrowing”, and give one example

A

call() and apply()

For function borrowing, when we want to use a method from one object, on another object!

69
Q

What method can we use when we want to do “function currying”? and why

A

We can use bind() method, because bind creates a new copy, and since its a new copy of the function object, the parameters that we pass sets the permaments values of these parameters when the copy is made!

function multiply(a, b) {

return a \* b;
}
var multipleByTwo = multiply.bind(this, 2);
console.log(multipleByTwo(4)); // 8

// The permament value of the second parameters b will be always 2

function multiply(b) {

var a = 2;

return a * b;
}

70
Q

What means, function currying?

A

Creating a copy of a function but with some preset parameters

71
Q
A
72
Q

To where points “this” in this case

function asdf() {

console.log(this);

}

A

To the global windows object.

73
Q

What we need to look at when we have the “this” keyword inside a function or object, to where it its pointig to?

Cómo nos podemos dar cuenta hacia donde apunta “this” cuando estamos invocando una función o metodo que esta usando a la palabra “this”.

A

Podemos fijarnos como es invocada esa funcion / metodo

el operador dot . es el que nos va a avisar hacia donde apunta, si tiene algo a su izquierda, es ese objeto, si se invoca sin punto, generalmente es el objecto global window

74
Q

Describe what is “this”(to where it points) in the following scenarios:

1) Inside a method of an object
2) Inside a function statement
3) Inside a function expression
4) An internal function, that it´s located inside a method of an object

A

This points to:

1) The object itself, the object that is calling the method
2) global variable window
3) global variable window
4) points to the global variable( lot of people think this is a bug)

75
Q

What is one of the purposes of making a function factory?

A

To have variables inside and secure in a closure

76
Q
A

We are making use of closures

greetEnglish, will return a function, will be a function where “var language = en”

Tx to closures, when greetEnglish is invoked, and it creates its own execution context it will still have the var “en” inside their clousure.

So greetEnglish is a function object whose closure points to language being en

77
Q

How can we create a function factory that is going to be using always the parameters that we pass?

What could happend if we pass variables as parameters to a function that return another function.

A

Using a function factory, and returning a function

When we return a function from a function, we are creating a closure.

When the returning function executes it creates itw own execution context, and its outer reference will point to the execution context that was created when invoking this function that returned this function (That´s a closure)

78
Q

What is the key to understandin closures?

Every time you call a function…

A

it gets itw own execution context,, and any function created inside of it will point (outer reference) to that execution context.

It wil point to the memory space, as if those other execution context hadn´t disappeared.

It knows which ones to point to properly, where these inner functions were created and when.

79
Q

For what are useful the function factories?

A

To stop passing parameters that could be used as default

var greetEnglish = makeGreeting(‘en’) // makeGreeting returns a function

greetEnglish(‘Bruno’, ‘De Angeli’); // greetEnglish has the returned funciton with the language set by default, tx to closures.

80
Q

How creates a closure, the JS or us?

A

TheJS creates the clousure, its a feature, we are just taking advantage of it.

81
Q

When the execution context that was actually running finish his execution and goes away…

1) What happens to that memory space where the variables and functions created inside of it live?
2) And how this relate to closure and “function factories”

A

1) It still continues in memory…
2) It relate to closure in the way that if we are returning a function from a function, and the first function execution context is gone, and we execute the second execution context(from the function that was returned from the first function) we still has a reference to the memory of the first function. We can still go down the scope chain and find it
- We can say that the execution context has closed IN its outer variables, the variables that it would normally have reference to anyway, even though those execution context are gone

That´s a closure, and that´s why we can make function factories

82
Q

What is the meaning of free variables, and when it comes natural to get them?

A

its natural to get them when we are inside a closure.

It is a variable that is outside a function, but that you have access to.