그가 아래 슬라이드에 나오는 자바스크립트 코드의 실행 결과를 정확하게 예측할 수 있는가? 정확한 정답을 알고 있다면 당신은 아마도 자바스크립트의 식스팩을 갖추고 있다고 말할 수 있을 것이다.
프리젠테이션에 나와있는 코드의 실행 결과는 다음과 같다.
1. Types & type coercion
> 5 - "4"
1
> 5 + "4"
54
> +!{}[true]
1
> +[1]
1
> +[1, 2]
Nan
> 7 - "a"
Nan
> 7 / 0
Infinity
2. Operators
> 5 + "4"
"54"
> 5 + null
5
> 4 == "4.00"
true
> null == undefined
true
> 0 == false
true
> 0 == null
false
> null == false
false
> typeof null == "object"
true
> typeof function () {} == "function"
true
3. Object * primitives
> var a = "string" undefined > a.length 6 > a.t = 3 undefined > a.t undefined
4. Function * constructors
> function f() {};
undefined
> var a = f, b = f(), c = new f, d = f(f);
undefined
> a
function f() {}
> b
undefined
> c
f {}
> d
undefined
5. Closures
> function add(a) { return function (b) { return a + b; }; }
undefined
> add(3)(4) == 7
true
6. Prototype
> function f() {};
undefined
> f.prototype.x = 3;
3
> var a = new f;
undefined
> a.x = 2;
2
> a.y = 1;
1
> var b = new f;
undefined
> f.prototype = { z: 0 };
Object {z: 0}
> var c = new f;
undefined
> b.constructor == f
true
> c.constructor == Object
true
나 역시 자바스크립트를 배우고 있는 입장이라 직접 실행해보기 전에는 이 모든 코드의 결과를 예측할 수 없었고, 현재도 이 코드의 실행 결과를 이해하지 못하는 경우가 대부분이다. 차근 차근 공부해서 왜 이런 결과를 나타내는지 이해하게 된다면 다시 포스팅을 하도록 하겠다 ;)
No comments:
Post a Comment