Functions and Scope Flashcards

1
Q

What are two ways to tell if something called myVar is an array?

A
  1. myVar instanceof Array;

2. Array.isArray(myVar); // not just isArray(var);

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

What does the toFixed() method do? (and on what type of value?)

A

It’s a method of the Number type and it returns a string with a specified number of decimal places. if n = 10.0092, n.toFixed(2) will return “10.01”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
var obj = new Object();
if (obj) { // will this take place? }
A

Yes, because objects convert to true when evaluated in Boolean expressions.

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

What is a function declaration compared to a function expression?

A

declaration: function myFunc(){ … }
expression: var myFunc = function() { … }

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

How could you get today’s day (e.g. “Monday”) from the date object?

A

You need to make an array with Sunday at 0 index and Saturday at 7 index.
Then use getDay() and find the day using your array.

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

Name the two RexEx methods that work with patterns:

A

exec() and test()

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

What is function declaration hoisting?

A

Before code begins running, the javascript engine “hoists” functions to the top of the execution context. That way, a function can be called even if it doesn’t appear in code until later. This only works with function declarations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
var arr1 = [1,2,3];
var arr2 = arr1;
arr1 = null;
what happens to arr2?
A

arr2 = [1,2,3];
arr1 and arr2 were both pointers to the same reference object (an array) but arr1 had it’s relationship that array broken.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
function f1(){ // do something; }
var f2 = f1;
f1 = null;
What happens if you call f2( ); ?
A

the original function will run.
When you set f1 to null, you broke the relationship between the pointer and the function but you didn’t change the function.

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

What does a for … in loop do and what does it look like?

A
It iterates over the enumerable properties in an object like this:
for (var prop in obj) { 
    // optionally, find only properties of this object, not the objects from which it inherits
    if (obj.hasOwnProperty(prop) { // do something
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you access the function you are in (e.g. for a factorial)?

A

Use the name of the function. Arguments.caller is deprecated.

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

What are the string regular expression methods?

A

match()
search();
replace();
split();

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

Describe the apply() vs call() function

A

Both take two arguments, the value of “this” and some optional arguments to be acted upon. Apply takes an arguments object or an array; call takes a list of arguments: someFunc.call(this, arg1, arg2, etc)

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

How can a primitive value like “cats” can have access to methods, e.g. “cats”.substring(1,3) or “cats”.split(‘’);

A

Every time a primitive value is read, an object of the corresponding primitive wrapper type is created. This wrapper allows access to methods like substring() but it then is immediately destroyed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
var s = String;
typeof s will be what?
A

“function”

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

string.replace() method take what argument(s)?

A

the pattern to match against and the thing to replace it with. This second argument (the replacement text) can be a function which will apply further logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
var s = new String();
typeof s will be what?
A

“object”

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

What is format for string matching methods?

A

stringName.match(regExPattern);

stringName.search(regExPattern);

19
Q

What are two ways to access the Global Object?

A
  1. Use window (window.property or window.method);

2. Return it from an anonymous function: global = function(){ return this; }();

20
Q

how do you find a random number between 1 and 10?

A

var rand = Math.floor(Math.random() * 10 + 1);

21
Q

Can you immediately invoke a function declaration as well as a function expression?

A

You have to wrap the function in parentheses and add () at the end:
(function(){ // do something }()). This works because parentheses can’t contain statements, so the parser knows to treat it like an expression.

22
Q

use apply() to apply cat.getInfo() method to dog object.

A

cat.getInfo.apply(dog);

This will run cat’s getInfo() method but will do it using dog as the “this” object.

23
Q

How does the bind() function work?

A
var bound = nameOfFunction.bind(nameOfObj);
You need to tie it to a variable like this or the binding just gets lost. You can now say:
bound(); // it will call nameOfFunction and the object we passed will be the "this" object.
24
Q

When should you use typeof rather than instanceof operator?

A

Use typeof for testing simple build-in types, like strings, booleans, and numbers:
99.99 instanceof Number; // false
typeof 99.99 == ‘number’; //true
Also for functions and null.

25
Q

When should you use instanceof rather than typeof operator?

A
Use if for complex built-in types such as RegEx, Array, Object. Also use for custom types:
var ClassFirst = function () {};
var ClassSecond = function () {};
var instance = new ClassFirst();
typeof instance == 'ClassFirst'; // false
instance instanceof Object; //true
instance instanceof ClassFirst; //true
26
Q
for (var i=0; i<10; i++){
 // what is i here? 
}
// what is i here?
Why does this happen?
A

i will be 9 in the first place and 10 in the second. Javascript holds onto variables that are set in for loops.
This happens because JS doesn’t have block-level scope, only function and global scope.

27
Q

What is an anonymous function?

A

A function that isn’t bound to an identifier. In js terms, a function without a name after the function keyword as in var f = function(){ }; // anonymous

28
Q

What is a closure?

A

A closure is a function that has access to variables from another function’s scope —usually because it’s inside of the other function.

29
Q

What is an execution context and what are it’s three forms?

A

An execution context is the environment / scope the current code is being evaluated in. There is a global one, an infinite number of contexts for functions, and the context created by eval().

30
Q

What is the Execution Context Stack?

A

Only one thing can happen at a time in javascript, so the interpreter puts execution contexts in a stack and works through them, top to bottom (LIFO).

31
Q

A function that is defined inside another function adds what into its scope chain?

A

The containing object’s activation object.

32
Q

What is an activation object?

A

It’s an object that holds variables that are local to a given execution context.

33
Q

A closure can still use variables that are local to the containing function, even if the containing function is set to null. How is this possible?

A

Because the closure has a reference to the containing function’s activation object, which won’t be destroyed (because of the closure).

34
Q

Does a closure store a reference to the containing function’s variables directly?

A

No, it stores a reference to the entire variable object. If the container has a loop that iterates over i, the closure will only point to the last value of i, even if created by one of the loop’s earlier iterations.

35
Q

Can an inner function ever have direct access to the “this” or “arguments” variables of a containing function?

A

No, you need to save a reference to either of these in a separate variable, such as that = this;

36
Q

Are function arguments passed by reference or by value?

A

They are only passed by value. You can use this with closures to “fix” the value of variables in the containing function.

37
Q

What does “this” point to when inside an anonymous function?

A

inner functions never have access to an outer function’s this or arguments variables, so it points to window. Use that or self to deal with this.

38
Q

What happens if you declare a variable more than once? (e.g. var i = 10; var i; // i is now what?)

A

Javascript ignores declarations on variables that have already been declared. i will be 10.

39
Q
var x = window.y;
if y is undeclared, what is the value of x?
A

undefined. Javascript doesn’t throw an error because y is part of a property lookup.

40
Q
// global scope
var name = "Bill;
delete name; 
console.log(name); // what happens?
A

Name still exists as “Bill” because you can’t delete properties of window that are declared using var keyword.

41
Q

What is the way to create a timer?

A
var timeoutID = setTimeout( function(){
    // do something
}, 1000); //use an id to reference the timeout.
42
Q

How can you end a timer?

A

clearTimeout(timeoutID); // you need a reference to the timeout ID.

43
Q

What scope do the timer methods belong to?

A

It belongs to window. “This” refers to the global scope, window.