Speaking JavaScript Flashcards

Rauschmayer

1
Q

What is the effect of this instruction?
myFunction(y>=0 ? y: -y){ return y}

What about this one:
f = myFunction(y>0 ? y: -y);

A

In the first case, you will get an error (Unexpected token >=) because functions do not accept conditional statements as parameters;

In the second case, that is a function statement so it can take a conditional expression as a call argument;

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

What is the effect of reading an unknown property?

A

A value of unknown property is undefined

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

What is the value of a missing parameter?

A

Undefined

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

What is the value of uninitialised variables?

A

Undefined

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

What is the logical value of undefined and null?

A

Both are false

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

What is the meaning of null?

A

It means “no object”. It is used where an object is expected

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

What is the difference between typeof and instanceof?

A

typeof( ) is meant to be used with primitive values, whereas instanceof( ) is meant to be used with objects

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

What is the value of: typeof(null)

A

It is ‘object’&raquo_space;> this is a Quirk

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

What is the logical value of an Object?

A

Objects are considered as “true”

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

What is the effect of: false && foo()

A

foo is never called

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

What is the effect of : true || foo( )

A

foo is never called

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

What is the logical value of: Infinity > NaN

A

false, because Infinity is bigger than any other number (except NaN)

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

What is the effect of:
let s = “abc”;
s.len++;
console.log(s.len);

A

undefined.

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

How do you enforce a function parity?

A

By checking the argument.length

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

How do you convert arguments to an array?

A

Array.prototype.slice.call(myArguments);

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

Can you remove elements from arguments?

A

No, because arguments is not an array

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

What is “this” pointing to in an object’s method?

A

It’s pointing to the actual object

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

What is the value of “this” inside a forEach method of an object’s property?

A
It is pointing to the global scope:
var jane = {
    name: 'jane',
    friends: 'Tarzan', 'Cheetah',
    yell: function( ) {
          this.friends.forEach(function(friend){
               console.log(this.name); // this point to global scope 
          })
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is “this” bound to in an object’s method? When is binding of “this” happening?

A

It is bound to that object (Crockford 1). The binding happens at invocation time.

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

How do the object’s public methods get their context?

A

They get their context from the invocation of “this” in an object

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

How is a function invoked when it is OT the property of an object?

A

It will be invoked as a function.

22
Q

What is “this” bound to in a function invocation pattern?

A

It is bound to the global object

23
Q

Can an object’s method employ an inner function to do any work? If yes, why, if not what is the solution?

A

the answer is no, because of the Crockford 2 “function invocation pattern”; the “this” in the inner function points to the global object. solutions: use “that” in the method, or use “bind”

24
Q

What is “this” bound to in a Constructor invocation pattern?

A

It is linked to the new Object (Crockford 3)

25
Q

What happens if you extract a method from an object in a separate function?

A

You will severe the “this” link to the object, so now “this” will point to the global scope

26
Q

What is “5 in myArray” instruction does?

A

it returns true if myArray has an element on position 5

27
Q

statements vs expressions&raquo_space;> what is the difference?

A

Expressions produce a value, statements perform an action

28
Q

What are object literals: expressions or statements?

A

Expressions

29
Q

What happens if you omit the outer parentheses below:

(function () { return ‘ac’}), and why?

A

You will get an error, as anonymous function declarations are not allowed

30
Q

What is “this” pointing to in a non-method function in “strict” mode?

A

undefined

31
Q

Why can you use “strict mode” to protect a constructor not be called without new?

A

Because if you don’t use “new” when calling the function, you actually call it in a non-method way, so if “strict” mode is active it be undefined

32
Q

What are the E6 primitive values?

A

boolean, numbers, strings, null, undefined, symbols

33
Q

What are the three characteristics of the primitive types? What are the three characteristics of objects?

A

Primitives:
1. They are always compared by value; (‘a’===’a’&raquo_space; true)
2. They are immutable (e.g. strings are immutable);
3. You cannot define your own set of primitive values
Objects:
1. They are compared by reference ({ } === { }&raquo_space;> false)
2. They are mutable by default;
3. They are user extensible;

34
Q

What is everything which is not a primitive in Javascript?

A

An object

35
Q

Enumerate different kinds of objects in Javascript

A

Plain objects (literals), Arrays,regexes

36
Q

What is the difference in terms of meaning for “undefined” and “null”? Give the use cases (when they appear) for both;

A

“undefined” means “no value” (neither primitive, nor object)&raquo_space;> uninitialized vars, missing params, missing properties; also functions return it if nothing is explicitely returned
“null” means “no object”&raquo_space;> it is used as a non-valued where an object is expected: a param, a member in a chain of objects

37
Q
Having the following function:
function returnFoo(x) { return x.foo}
What is the return value for the following:
1. returnFoo(true)
2. returnFoo(0)
3. returnFoo(null)
4. returnFoo(undefined)
A
  1. undefined
  2. undefined
  3. TypeError: cannot read property “foo” of null
  4. TypeError: cannot read property “foo” of undefined
38
Q

What is the last element in a prototype chain and how do you get it?

A

null

Object.getPrototypeOf(Object.prototype)

39
Q

How do you check for null and how do you check for undefined?

A

if (x === null)

if (x === undefined)

40
Q

How do you check for either null or undefined?

A

if (x!== undefined && x!==null) // does x have a value
if(x === undefined || x === null) // is x a non-value

41
Q

What are the javascript values that are considered “false”

A

false, 0, nan, ‘’

42
Q

What is 5+null and why?

A

5, because null coerced to a number is 0

43
Q

What is 5+undefined and why?

A

NaN, because undefined coerces to NaN

44
Q

What is the recommended method to converse 123 into a string?

A

String(123)

45
Q

What is the logical value of:

‘abc’ instanceOf String

A

false, as primitives are not equal to their wrappers

46
Q

What are the falsy values?

A

undefined, null
false
0, NaN
‘’

47
Q

What are considered as “truthy” values in javascript?

A

Everything which is not falsy

48
Q

What is the boolean value of an object?

A

truthy

49
Q

What is string(null)

A

‘null’

50
Q

What is String(false)

A

‘false’

51
Q
What do you get when you use:
let o = Object.create(null,{a:{value:1}}) and then use:
o.\_\_proto\_\_ ?
A

undefined