Karteikarten
Javascript constructs
Karten in diesem Stapel (22)
- 1What is an: immediately invoked function expression (IIFE)?(function IIFE(){
console.log( "Hello!" );
})();
// "Hello!" - 2In which scope is JavaScript? Lexical or Dynamic?Lexical
- 3What are the drawbacks of anonymous functions?No useful name to display in stack traces, which can make debugging more difficult.
Anonymous functions omit a name, which is often helpful in providing more readable/understandable code - 4Explain hoistingWhen Javascript compiles all of your code, all variable declarations using var are hoisted/lifted to the top of their functional/local scope (if declared inside a function) or to the top of their global scope (if declared outside of a function) regardless of where the actual declaration has been made.
- 5Explain closuresClosure is when a function can remember and access its lexical scope even when it’s invoked outside its lexical scope.
If you call function A with var C and return a function nested within A then you can access var C which would ordinarily not be accessible. - 6What is coercion?Forcing a variable into another type
- 7What is unusual about null?typeof null === "object"; // true
It would have been nice (and correct!) if it returned "null", but thisoriginal bug in JS has persisted for nearly two decades, and will likelynever be fixed because there’s so much existing web content that relieson its buggy behavior that “fixing” the bug would create more “bugs”and break a lot of web software. - 8How do you test for null?If you want to test for a null value using its type, you need acompound condition:
var a = null;
(!a && typeof a === "object"); // true - 9Arrays native to JS, so are they a special type?typeof [1,2,3] === "object"; // true
It’s most appropriate to think of them also as a“subtype” of object, in this case with the additional characteristics of being numerically indexed (as opposed to just being string-keyed like plain objects) and maintaining an automatically updated .length property. - 10In JavaScript, do variables have types?Variables don’t have types — values have types. Variables can hold any value, at any time.
- 11What are all global variables properties of?All global variables are also properties of the global object, which in the browser is basically the window object. So, the above checks could have been done (quite safely) as:
if (window.DEBUG) { // ..}
if (!window.atob) { // ..} - 12Can you add string keys/properties to arrays?Yes but generally it’s not a great idea to add string keys/properties to arrays.
Use objects for holding values in keys/properties, and save arrays for strictly numerically indexed values. - 13Explain how to use: indexOf(..) concat(..) forEach(..) slice(..) map(..) join(..)The indexOf() method returns the position of the first occurrence of a specified value in a string.
This method returns -1 if the value to search for never occurs. - 14How do you pass arguments by reference?Whereas JavaScript primitive variables are passed to functions by value, JavaScript objects are passed by reference. What this means is that if you pass an object as an argument to a function, any changes to that object within the function will also change the value outside of the function.
- 15How do you call a function with more arguments than parameters or an unknown number of parameters?When you don’t know how many arguments will be passed into a function, you can use the argument object, which is built-in to functions by JavaScript, to retrieve all the arguments and make use of them.
The Arguments object contains an array of all the arguments passed to a function. By looping through the array, you can make use of every argument, even if the number of arguments may change each time the function is called.
for (i = 0; I < arguments.length; i++) {
welcome = welcome + arguments[i] + ““;
} - 16Give examples of the two ways that objects can be instantiated.Objects come in two forms: the declarative (literal) form and the constructed form.The literal syntax for an object looks like this:
var myObj = { key: value // ...};
The constructed form looks like this:
var myObj = new Object();
myObj.key = value;
The constructed form and the literal form result in exactly the same sort of object. The only difference really is that you can add one or more key/value pairs to the literal declaration, whereas with constructed-form objects, you must add the properties one by one.
It’s extremely uncommon to use the “constructed form” for creating objects as just shown. You would pretty much always want to use the literal syntax form. The same will be true of most of the built-in objects - 17Is every type in JavaScript an object?Simple primitives (string, boolean, number,null, and undefined) are not themselves objects.
null is sometimes referred to as an object type, but this misconception stems from a bug in the language that causes typeof null to return the string "object" incorrectly (and confusingly). In fact, null is its own primitive type. - 18What are the object subtypes?There are several other object subtypes, usually referred to as built-in objects. For some of them, their names seem to imply they are directly related to their simple primitive counterparts, but in fact, their relationship is more complicated:
String
Number
Boolean
Object
Function
Array
Date
RegExp
Error
There are a few special object subtypes, which we can refer to as complex primitives.
function is a subtype of object (technically, a “callable object”).Functions in JS are said to be “first class” in that they are basically just normal objects (with callable behaviour semantics bolted on), and so they can be handled like any other plain object.
Arrays are also a form of objects, with extra behaviour. The organisation of contents in arrays is slightly more structured than for general objects. - 19What is the difference between property access and key access?var myObject = {
a: 2
};
myObject.a; // 2
myObject["a"]; // 2
To access the value at the location a in myObject, we need to use
either the . operator or the [ ] operator.
The .a syntax is usually referred to as “property access,” whereas the ["a"] syntax is usually referred to as “key access.” In reality, they both access the same location and will pull out the same value, 2, so the terms can be used interchangeably.
The main difference between the two syntaxes is that the . operator requires an Identifier-compatible property name after it, whereas the[".."] syntax can take basically any UTF-8/Unicode-compatible string as the name for the property. To reference a property of the name"Super-Fun!", for instance, you would have to use the ["Super-Fun!"]access syntax, as Super-Fun! is not a valid Identifier property name. - 20What are the features of arrays?Arrays assume numeric indexing, which means that values arestored in locations, usually called indices, at positive integers,such as 0 and 42.
Arrays are objects, so even though each index is a positive integer, you can also add properties onto the array.
NOTE: This is bad idea because arrays have behaviour and optimisations specific to their intended use, and likewise with plain objects. Use objects to store key/value pairs, and arrays to store values at numeric indices. - 21How do you create setters and getters for an object?var myObject = {
// define a getter for `a`
get a() {
return this._a_;
},
// define a setter for `a`
set a(val) {
this._a_ = val * 2;
}
};
myObject.a = 2;
myObject.a; // 4 - 22Who do you check if an object has a property?var myObject = {a: 2};
("a" in myObject); // true
("b" in myObject); // false
myObject.hasOwnProperty( "a" ); // true
myObject.hasOwnProperty( "b" ); // false
Informationen
- Beschreibung
- Javascript Karteikarten am Javascript constructs, erstellt von Ben Parish am 09/05/2019.
- Erstellt von
- Ben Parish
- Aufrufe
- 1
- Bewertung