Speaking JavaScript Flashcards
Rauschmayer
What is the effect of this instruction?
myFunction(y>=0 ? y: -y){ return y}
What about this one:
f = myFunction(y>0 ? y: -y);
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;
What is the effect of reading an unknown property?
A value of unknown property is undefined
What is the value of a missing parameter?
Undefined
What is the value of uninitialised variables?
Undefined
What is the logical value of undefined and null?
Both are false
What is the meaning of null?
It means “no object”. It is used where an object is expected
What is the difference between typeof and instanceof?
typeof( ) is meant to be used with primitive values, whereas instanceof( ) is meant to be used with objects
What is the value of: typeof(null)
It is ‘object’»_space;> this is a Quirk
What is the logical value of an Object?
Objects are considered as “true”
What is the effect of: false && foo()
foo is never called
What is the effect of : true || foo( )
foo is never called
What is the logical value of: Infinity > NaN
false, because Infinity is bigger than any other number (except NaN)
What is the effect of:
let s = “abc”;
s.len++;
console.log(s.len);
undefined.
How do you enforce a function parity?
By checking the argument.length
How do you convert arguments to an array?
Array.prototype.slice.call(myArguments);
Can you remove elements from arguments?
No, because arguments is not an array
What is “this” pointing to in an object’s method?
It’s pointing to the actual object
What is the value of “this” inside a forEach method of an object’s property?
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 }) } }
What is “this” bound to in an object’s method? When is binding of “this” happening?
It is bound to that object (Crockford 1). The binding happens at invocation time.
How do the object’s public methods get their context?
They get their context from the invocation of “this” in an object
How is a function invoked when it is OT the property of an object?
It will be invoked as a function.
What is “this” bound to in a function invocation pattern?
It is bound to the global object
Can an object’s method employ an inner function to do any work? If yes, why, if not what is the solution?
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”
What is “this” bound to in a Constructor invocation pattern?
It is linked to the new Object (Crockford 3)
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
What is “5 in myArray” instruction does?
it returns true if myArray has an element on position 5
statements vs expressions»_space;> what is the difference?
Expressions produce a value, statements perform an action
What are object literals: expressions or statements?
Expressions
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
What is “this” pointing to in a non-method function in “strict” mode?
undefined
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
What are the E6 primitive values?
boolean, numbers, strings, null, undefined, symbols
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’»_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 ({ } === { }»_space;> false)
2. They are mutable by default;
3. They are user extensible;
What is everything which is not a primitive in Javascript?
An object
Enumerate different kinds of objects in Javascript
Plain objects (literals), Arrays,regexes
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)»_space;> uninitialized vars, missing params, missing properties; also functions return it if nothing is explicitely returned
“null” means “no object”»_space;> it is used as a non-valued where an object is expected: a param, a member in a chain of objects
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)
- undefined
- undefined
- TypeError: cannot read property “foo” of null
- TypeError: cannot read property “foo” of undefined
What is the last element in a prototype chain and how do you get it?
null
Object.getPrototypeOf(Object.prototype)
How do you check for null and how do you check for undefined?
if (x === null)
if (x === undefined)
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
What are the javascript values that are considered “false”
false, 0, nan, ‘’
What is 5+null and why?
5, because null coerced to a number is 0
What is 5+undefined and why?
NaN, because undefined coerces to NaN
What is the recommended method to converse 123 into a string?
String(123)
What is the logical value of:
‘abc’ instanceOf String
false, as primitives are not equal to their wrappers
What are the falsy values?
undefined, null
false
0, NaN
‘’
What are considered as “truthy” values in javascript?
Everything which is not falsy
What is the boolean value of an object?
truthy
What is string(null)
‘null’
What is String(false)
‘false’
What do you get when you use: let o = Object.create(null,{a:{value:1}}) and then use: o.\_\_proto\_\_ ?
undefined