JavaScript Random 1 Flashcards
What is pass by value?
In Pass by Value, Functions are called by directly passing the value of the variable as the argument. Changing the argument inside the function doesn’t affect the variable passed from outside the function.
What is pass by reference?
In Pass by Reference, Functions are called by directly passing the reference/address of the variable as the argument. Changing the argument inside the function affects the variable passed from outside the function.
What type of variables are passed by value in JavaScript?
JavaScript is always pass by value for String or Number so changing the value of the variable never changes the underlying primitive.
What type of variables are pass by reference in JavaScript?
In JavaScript objects and arrays follow pass by reference.
How do you access an objects property using a variable?
Use bracket notation.
const myObject = { name: "Thomas Mann", age: "32" }
const prop="name"; console.log( myObject[prop] ); // Output "Thomas Mann"
const prop="age"; console.log( myObject[prop] ); // Output "32"
What does Array.prototype.filter() do?
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
How would you use filter to remove words from an array that are 4 letters long or less?
const arr = [‘alphabet’, ‘beef’, ‘chardonay’];
const filteredArray = arr.filter((word)=> word.length>4); // If the test on the element returns true, the element goes in the new array
console.log(filteredArray); // Output ["alphabet","chardonay"]
What phrase did I come up with that helps me remember how filter works?
If the filters test returns true, the current element goes in the new array.
How do you prevent an object’s properties from being modified?
You use the Object.freeze( ) method.
const obj = { prop: 42 };
Object.freeze(obj);
obj.prop = 33; // Throws an error in strict mode
console.log(obj.prop); // expected output: 42
Will trying to modify a object that has been frozen throw an error?
Sometimes.
Nothing can be added to or removed from the properties set of a frozen object. Any attempt to do so will fail, either silently or by throwing aTypeErrorexception (most commonly, but not exclusively, when instrict mode).
What is a shallow freeze?
The result of callingObject.freeze(object)only applies to the immediate properties ofobjectitself and will prevent future property addition, removal or value re-assignment operationsonlyonobject. If the value of those properties are objects themselves, those objects are not frozen and may be the target of property addition, removal or value re-assignment operations.
Is this a deep freeze?
Object.freeze(object)
No this is a shallow freeze.
How do you do a deep freeze?
You have to write a function to recursively go through an object’s properties looking for object’s or arrays that also need freezing.
Are functions in JavaScript Objects?
Yes!
In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects.
What does this highlight?
function myFunc(){ let a=2; };
console.log(myFunc.name);
That functions are object’s in JavaScript. Here we are accessing the name property of the function object.