www.rozumim.cz

What I've learned ~ 2

JavaScript

You can’t return a value from Array.forEach

What will print the following piece of code?

1
2
3
4
5
6
7
8
9
10
var r = [1, 2, 3].forEach(
    function(x){
      if (x % 2 === 0) {
        return false;
      } 
      return true;
    }
  );

console.log('r=', r);

The value r is undefined.

1
$ r=undefined

In this specific situation you would probably want to use the Array.some.

React

Use this.state = { .. } only in constructor

Everywhere else use this.setState({ .. }). State set directly using this.state = { .. } may be easily ignored and overridden by state set correctly using the function this.setState({ .. }) as you can see in the example.

Java

HttpURLConnection restricts headers by default

Some HTTP headers are silently removed when you use sun.net.www.protocol.http.HttpURLConnection. You can allow them by setting:

1
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");

For more details see this answer on stack overflow.

11. 10. 2015, kategorie:
comments powered by Disqus