Table of contents
JavaScript has ready-made functions that come with the language; we will go through a few of them here:
Functions for objects
Object.freeze()
Using freeze()
will ensure that an object and its properties become read-only:
const myObject = {
myProp1: 2,
myProp2: "my value"
};
Object.freeze(myObject);
myObject.myProp1 = 3;
myObject.myProp2 = "this won't become the value"
// myObject and its properties won't change
However, if a property contains a (nested) object, then that property can still change:
const myObject = {
myProp1: 2,
myProp2: {
myNestedProp: "my value"
}
};
Object.freeze(myObject);
myObject.myProp2.myNestedProp = "your value";
// the program will acknowledge this change to myNestedProp
Objects for arrays
every()
The every()
method determines if all elements in an Array
type meet a specific condition:
const myArray = [1, 3, 9, 23, -5]
const yourArray = [2, 3, 4, 5]
// condition
const isPositive = (number)
=> number > 0
myArray.every(isPositive)
// false
yourArray.every(isPositive)
// true