Then, we want to find the index of our least favorite food: the string with the cheese "□"! In order to find the index of the cheese, we can use the indexOf method. The variable favoriteItem gets reassigned with the value "□". We no longer want the grapes as our favorite item! Instead, we want to make the avocado "□" our favorite item. This means that the value of favoriteItem is a copy of the item "□" in the groceries array, without containing any references to the groceries array. Since the string "□" is a primitive data type (it's a string! □), the string gets passed by value. We assign the variable favoriteItem to that returned value. The find method returns the value of the item in the array that we're trying to find: the string with the grapes in this case "□". We have a list of groceries! One item in this list is our favorite item, and one item in this list is our least favorite item.įirst, we want to get the value of our favorite item, the grapes! One way of doing this, is by using the find() method. In this case, we give the person variable the value of a reference (pointer) to the object, that object gets returned when logging the first element! Instead, we're giving it the value of a reference (or actually pointer) to that object in memory! ⚡️ Whenever we declare a set a variable equal to an object, we're not actually giving that variable the value of that object. The bind method returns a copy of the bound function, meaning that we're simply logging a new, copied function □ The call method executes the (bound) function immediately, which results in Lydia is 21. call() executes the bound function immediatelyįirst, we log sayHi.call(person, 21).bind() only returns a copy of the bound function.In this example, we're saying that the this keyword within the sayHi function should refer to the person object by calling both bind and call on the sayHi function □Īlthough the bind() and call() methods both allow us to specify which object the this keyword should refer to, there's a tiny difference: With the bind() and call() method, we can decide to which object the this keyword should refer. Sometimes I have to do things a certain way in order to be able to show certain behavior that may happen, and explain it that way □ Of course, the examples are still minimal and don't show "the best/most performant way to do it". A long list of (advanced) JavaScript questions, and their explanations ✨
0 Comments
Leave a Reply. |