Objects and Functions Flashcards
What are the 3 things an Object can have attached to it?
Objects have properties and methods and properties can be either a primitive value or another object.
What is a namespace?
A container for variables and functions. Typically to keep variables and functions with the same name separate. Javascript does not have namespaces due to the nature of objects.
What is faking a namespace and what is it used for?
Other languages have namespaces, which basically define a space for sets of variables and functions. JS does not have this so we can fake a namespace by setting up an object to perform the same function as a namespace. This is done in both cases to prevent variables and functions with the same from overriding each other. See below.
What will this code output and why?
Since dot operators are still operators they also must obey the rules of associativity, which is left-to-right.
So, first it would look for english.greetings and wouldn’t find anything. So, english.greetings is undefined and so it wouldn’t be able to find greet because it would be looking for undefined.greet then.
This is why these can’t be created on the fly. It must be set by declaring english.greetings = {}; or just by initializing english like this:
var english = { greetings: { greet: ‘Hello!’ } };
How is JSON different than Object literal syntax?
JSON is technically a subset of the Object syntax, but has stricter rules. It is technically not a part of JS, but JS does come with some built-in functionality to work back and forth with it, like JSON.stringify or JSON.parse
In JSON all properties have to be wrapped in quotes, which is also valid for Object literal syntax in JS, but not required in JS. So, anything that is JSON valid is going to be valid in JS Object literal syntax, but not everything that is valid Object literal syntax is valid JSON.
JSON was created as a replacement for XML, which was the initial method for sending information across the internet.
How do you convert a JS object to JSON?
JSON.stringify will convert a JS Object to a JSON string. In the image below, line 6 results in line 8.
How do you convert a JSON object into a JS object?
JSON.parse will convert a JSON object into a JS object.
What is a function?
A function is actually just an object, but it’s a special object. So, in addition to the properties and methods that a normal object can have, it also has name and code values attached to it. Importantly, the code value is invokable.
What are first class functions?
First class functions can do everything you can do with other types (strings, objects, numbers, etc.) - assign them to variables, pass them around or create them on the fly. JS isn’t the only language that has first class functions, btw.
First class functions change the way you can program and open up completely different approaches.
What’s the difference between a statement and an expression?
A statement just does work but an expression returns value.
An if statement, for example, does not return anything. The expression placed inside of it for evaluation does return a value, but the if statement just does work and doesn’t return anything.
You can’t set a statement as a variable like var a = if( b > c){…};
What’s an anonymous function?
A function that doesn’t have a name. Example below:
function (){
console.log(‘hi’);
}
What will this code output and why?
greet is a function(function statement) and so it is hoisted in its entirety and is available for invocation during the execution phase. But, anonymousGreet is set to a variable(function expression), which is only set to undefined in memory and so cannot be invoked during execution phase since we’re invoking it before we’ve defined the function below it in the code.
What does this code return?
8
What does this code return?
3
What does this code return?
3
What does this code return?
[Type Error: bar is not a function]
How is a function statement different from a function expression?
The main difference between a function expression and a function statement is the function name, which can be omitted in function expressions to create anonymous functions. A function expression can be used as a IIFE (Immediately Invoked Function Expression) which runs as soon as it is defined.
What will this code output and why?
This happens because primitive values are stored by variables by value. So, each time a reference is made to a value, it is a new reference or a copy. Changing the initial value will not affect other references because they are separate copies - it would be like making a copy of a document and then making edits to the first one: the second copy remains intact without the edits.