Let 바인딩
Quick overview: Let Bindings
Reason의 let 바인딩은 값과 이름을 연결 한다. 다른 말로는 "변수 선언"이라고 불린다. 바인딩은 immutable하고 이후의 코드에게 참조 될 수 있다.
let greeting = "hello!";
let score = 10;
let newScore = 10 + score;
노트: 만약 javascript에서 왔다면 이 바인딩은 var
orlet
처럼이 아니라 const
처럼 행동한다.
바인딩은 불변인다
Reason 의 let 바인딩은 "불변"이다. 생성 된 후 다시 변경이 불가능하다.
let x = 10;
/* Error: Invalid code! */
x = x + 13;
Binding Shadowing
Bindings can be shadowed to give the appearance of updating them. This is a common pattern that should be used when it seems like a variable needs to be updated.
let x = 10;
let x = x + 10;
let x = x + 3;
/* x is 23 */
블록 스코프
바인딩은 {}
를 사용해서 스코프가 지정된다.
let message = {
let part1 = "hello";
let part2 = "world";
part1 ++ " " ++ part2
};
/* `part1` and `part2` not accessible here! */
블록의 마지막 줄은 암시적으로 반환된다.
Mutable Bindings
If you really need a mutable binding then check out the ref
feature.