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
What happens if you extract a method from an object in a separate function?
You will severe the "this" link to the object, so now "this" will point to the global scope
26
What is "5 in myArray" instruction does?
it returns true if myArray has an element on position 5
27
statements vs expressions >>> what is the difference?
Expressions produce a value, statements perform an action
28
What are object literals: expressions or statements?
Expressions
29
What happens if you omit the outer parentheses below: | (function () { return 'ac'}), and why?
You will get an error, as anonymous function declarations are not allowed
30
What is "this" pointing to in a non-method function in "strict" mode?
undefined
31
Why can you use "strict mode" to protect a constructor not be called without new?
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
What are the E6 primitive values?
boolean, numbers, strings, null, undefined, symbols
33
What are the three characteristics of the primitive types? What are the three characteristics of objects?
Primitives: 1. They are always compared by value; ('a'==='a' >> 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 ({ } === { } >>> false) 2. They are mutable by default; 3. They are user extensible;
34
What is everything which is not a primitive in Javascript?
An object
35
Enumerate different kinds of objects in Javascript
Plain objects (literals), Arrays,regexes
36
What is the difference in terms of meaning for "undefined" and "null"? Give the use cases (when they appear) for both;
"undefined" means "no value" (neither primitive, nor object) >>> uninitialized vars, missing params, missing properties; also functions return it if nothing is explicitely returned "null" means "no object" >>> it is used as a non-valued where an object is expected: a param, a member in a chain of objects
37
``` 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) ```
1. undefined 2. undefined 3. TypeError: cannot read property "foo" of null 4. TypeError: cannot read property "foo" of undefined
38
What is the last element in a prototype chain and how do you get it?
null | Object.getPrototypeOf(Object.prototype)
39
How do you check for null and how do you check for undefined?
if (x === null) | if (x === undefined)
40
How do you check for either null or undefined?
if (x!== undefined && x!==null) // does x have a value if(x === undefined || x === null) // is x a non-value
41
What are the javascript values that are considered "false"
false, 0, nan, ''
42
What is 5+null and why?
5, because null coerced to a number is 0
43
What is 5+undefined and why?
NaN, because undefined coerces to NaN
44
What is the recommended method to converse 123 into a string?
String(123)
45
What is the logical value of: | 'abc' instanceOf String
false, as primitives are not equal to their wrappers
46
What are the falsy values?
undefined, null false 0, NaN ''
47
What are considered as "truthy" values in javascript?
Everything which is not falsy
48
What is the boolean value of an object?
truthy
49
What is string(null)
'null'
50
What is String(false)
'false'
51
``` What do you get when you use: let o = Object.create(null,{a:{value:1}}) and then use: o.__proto__ ? ```
undefined