hjertnes.blog

Arrow Functions

24.10.2017 02:00

We use them all over, not just because they are shorter to write, but also because they look cool, and you don’t need to bind this for them to work with this. I’m going to write about ‘this’ some other time. The biggest difference between a regular function and a arrow function is that the former has it’s own ‘this’, which means that you need to bind to ‘this’, if it needs access to it, while an arrow function on the other hand shares it with it’s parent. You can write an arrow function in three different forms: expression, statements and object litteral.

```text A object literal is a way to use the simple form of the expression to make an arrow function return a object. It is this way because the object syntax {} would not work in an expression because that’s the syntax of using a full arrow function. ```

```text What to use where? ```

```text I use what ever variant that end up with the code that is the easiest to understand. I try really hard to use expression where ever I can. And to use Object Litterals when I need to return objects and to only to use a full function when it is the only way. ```

Value and reference types

23.10.2017 02:00

In my previous post about the .forEach method I said that you can mutate the values of the entities of the array you are running it on. This is only kind of true. You will be able to mutate reference types but not value types. A reference type is: functions, objects and arrays. Values types are: numbers, strings bool etc. The difference is that, when you pass a object to a function you can actually mutate the object, while a number would be passed as a value, and not be mutated. Look at the two examples below: By reference:

```text By value: ```

Review

22.10.2017 02:00

```text This have been a tool in my belt for a very long time. Fluid App is the fastest and easiest way to turn a Web Site into a App on OS X. ```

```text Why would you do that? ```

```text Well, I personally prefer to have som web apps as a separate window outside my browser, because I don’t want it to disappear between all of the way too many tabs I have open, and because some of them are in use all of the time. ```

```text It’s only five bucks. And is well worth the money. ```

```text I use Fluid, even though I cloud fire up Xcode and do the same thing there. But why would I spend like an hour or something to get it to work right, when I could spend a few minutes doing it in fluid? ```

Understanding ForEach

20.10.2017 02:00

ForEach is the second of two different methods that exists in the Array prototype for looping over a array. The difference between the two is that map does not mutate the original array, while forEach does. Let’s take a look at the example below:

It looks and works <em>kind of</em> the same. The big difference is that forEach will <em>mutate</em> the orignal array, but it will not return a new array. This means that it will <em>change</em> it according to the code you write, but it will replace it with undefined if you try to do something like <code>myList = myList.forEach(...)</code> </div>

Understanding Map

19.10.2017 02:00

Map is one of two functions in the Array prototype that let you iterate over an array. Just like reduce, filter and fine it takes either a expression or a function as its first parameter. Look at the example below:

```text The way map works is that it runs the function you define in the first parameter once per element in the array you run it on. A map should always return something, and it never mutates the data you run it on.
This means that row three of our example will not do anything, because map will produce a new array, that you need to replace your first one with, if that’s what you want.
To work with array in this way is less error prone and have less if not zero side affects, than to mutate it using for example a for loop. But it might be slower; I personally think that most people that run into this kinde of performance trouble probably have some other "issues" in their code.
I jumped in with both feet as soon as I discovered map, and never looked back. ```

Understanding filter

18.10.2017 02:00

There are three javascript methods I use more than anything else: map, find and filter. Find and filter are kind of two sides of the same coin. The only difference is that find returns either the element it matched with or undefined while filter returns either an array with all the matches or an empty array. The way a filter works, is that you give it either a function or a simple expression as its only parameter, and all the elements that return true is included while everything that return false is not. Look at the example below:

```text Remember: filter will not mutate your original array, so you need to replace it if that is what you intended to do.
The thing I love about filters is how easy it is to make a bunch of small filter expressions, instead of large and complex ones, and you’ll end up with code that is very easy to read and maintain.
I don’t even want to think about how we did it before filter. Probably a bunch of for loops and adding stuff that matched to a new array and then replacing them. ```

Understanding find

17.10.2017 02:00

Find is a method you can use on any array in Javascript. You use to find a single element in a array. It is very similar to for example filter(covered in a future post), the big difference between the two is that find returns a single element or undefined; while filter returns an array of matches or a empty array if none are found. For example like this:

```text You provide it with either a full function, arrow function or a simple expression, and it returns the first match or undefined; if you don’t have any matches.
This is usually how I did it before I discovered .find: ```

```text Or even worse, some horrible for loop with a break. ```

Understanding Reduce

16.10.2017 02:00

```text Reduce is a in the Array.prototype; which means you can run it on any array or list. It is a method that lets you reduce a list or collection down to a value. ```

```text You have probably written something like this more times than you care to ```

```text The next time you’re about to do that, is the perfect time to figure out how reducers work. Because those four lines can be replaced by a single line using a reducer. ```

```text This is how it works. Reduce takes a function as its argument, and you can also provide an optional default value. I almost always do that. The function takes two arguments one accumulator and the currentValue. The accumulator is the result of what the reducer have done up to this point, and the currentValue is the value of the item in the array. ```

```text For example, the first time it is called in our simple example above the parameters will be (0, 1) and then it will be (1, 2) and so on. ```

```text I personally think that reducers is a great way to turn a array of values or objects into a single value. They produce less code than looping over them. And as a bonus: they have far fewer side affects. ```

OS X

15.10.2017 02:00

```text There are a few tasks I have to break down into small chunks. One of them are maintaining the “Audit” section of 1Password, another one is to manage Notifications on OS X. ```

```text The short version is: I don’t want any. ```

```text So. The way you disable notifications on OS X, is more or less the same as iOS. You have to you through every singe app and disable everything. And it is a major pain in the ass. ```

```text My problem with it is two fold. First of all, why not let me mark all of them and do what ever I want to do with either all or any selection of apps. Second, why do I have to click a million different things to just disable notifications? ```

```text This is one of the areas where the iOS influence on OS X was wrong. Not the inclusion of it, but rather that the interface are lacking every ability to work efficient. ```

Welcome

11.10.2017 02:00

Too many Javascript blogs isn’t possible, right?

```text The pitch for this blog is a collection of short and easy to understand explanation of something related to Javascript. I might sneak in some related topics here and there. But the core focus will be javascript; both as a front end and back end language. ```

```text I’ll publish one post per weekday. ```

```text Enjoy. ```