Javascript 함수(function) 개념, 호출패턴, this, prototype, scopeYoung-Beom RheeJavascript의 함수(Function)의 기본개념과, 호출패턴에 따라 달라지는 this, 상속을 가능하게 해주는 prototype, 다른 언어와 다른 scope에 대해 알아봅니다.
Javascript introduction, dynamic data type, operatorYoung-Beom Rhee대상 : Javascript를 언어로 처음 접하는 개발자
범위 : Javascript의 흐름, 문맥(Context)를 모르면 이해할 수 없는 Dynamic typing(동적 데이터 타입), 멘붕을 마음껏 선사할 헷갈리는 연산자
Javascript 실행 가능한 코드(Executable Code)와 실행 콘텍스트(Execution Context), Lexical En...Young-Beom RheeJavascript의 함수가 실행될때 실행 가능한 코드(Executable Code)와 실행 콘텍스트(Execution Context)에 설정되는 것들과 주의할 점들
프론트엔드스터디 E03 - Javascript intro.Young-Beom RheeJavascript 흐름, 동적데이터타입(Dynamic typing), 연산자, 객체, 배열, 동적 파라미터, Script 선언위치 등 Javascript를 처음 접했을때 꼭 이해해야 되는 내용들에 대해서 알아봅니다.
골때리는 자바스크립트 발표자료욱진 양Sorry. for koreans only. I'll add english short description. or just visit http://wtfjs.com
아오 썅.
정정사항입니다.
객체 동일비교는 항상 valueOf 결과물로 나온 비교로 하는 데다가,
객체와 배열은 valueOf 해봐야 객체로 나오기 때문에
객체와 배열은 == 하나 === 하나 같은 비용으로 비교합니다.
[1,2,3]==[1,2,3] 은 false가 되므로
[1,,,2]==[1,undefined,undefuned,2] 는 무조건 false로 나옵니다.
iOS 메모리관리Changwon National University이 자료는 이영록강사님이 2011년 iOS 개발자 포럼에서 발표한 내용을 약간 개선하고 정리한 자료입니다.
iOS의 메모리관리 기법은 retain count라고 하는 독특하면서도 효율적인 방법을 사용하며 최근에 발표된 Xcode에서는 Automatic Reference Counting(ARC) 기법을 통해 release를 사용하에 객체를 소거하는 불편함을 많이 개선하였습니다.
본 자료에서는 이러한 점에 대한 비교적 상세한 설명을 담고 있습니다.
[IT기술칼럼#2] 고급자바스크립트 for AngularJS, React_고급자바스크립트,AngularJS,React전문교육학원탑크리에듀(구로디지털단지역3번출구 2분거리)탑크리에듀(www.topcredu.co.kr), 송석원 교수의 IT기술칼럼#2
자바스크립트는 ES6버전에서 class 키워드를 도입했습니다. 자바스크립트는 객체지향 언어였지만 다른 객체지향언어처럼 사용하기에는 많은 불편함이 있었습니다. 심지어 자바스크립트는 객체지향언어라고 볼 수 없다고 하는 분들도 있었을 정도였죠. 최근에 선풍적인 인기를 얻고 있는 React, Angular 같은 기술은 클래스 문법을 채택했습니다. 따라서, 클래스 문법에 대한 이해가 깊다면 새로운 기술을 습득할 때 많은 도움이 될 것 입니다.
다른 객체지향 언어와 자바스크립트의 차이를 이해하지 못하고 사용하게 되면 많은 문제점이 발생합니다. 안타깝지만 자바스크립트를 사용하고 있는 개발자들 중에서도 제대로 이해하고 사용하시는분은 많지 않은 듯 합니다.
이를 해소하기 위해서 새로 도입한 문법이 class 키워드로 대표되는 클래스 문법입니다. 객체지향 개발방법의 익숙한 개발자들을 위해서 자바스크립트가 새로운 문법을 도입한 것 입니다. 개인적으로 “JavaScript: The Good Parts”의 저자이신 Douglas Crockford 님께서 이를 어떻게 생각하는지 궁금합니다. 자바스크립트를 자바처럼 사용하는 것에 우려를 표하셨었는데, 그 사이 많은 시간이 지났으므로 입장이 바뀌었을지 궁금하군요. 선도적인 개발자들은 ES6에서 도입한 클래스 문법이 좋은 것인가 나쁜 것인가를 갖고 토론을 하기도 합니다.
골때리는 자바스크립트 발표자료욱진 양Sorry. for koreans only. I'll add english short description. or just visit http://wtfjs.com
아오 썅.
정정사항입니다.
객체 동일비교는 항상 valueOf 결과물로 나온 비교로 하는 데다가,
객체와 배열은 valueOf 해봐야 객체로 나오기 때문에
객체와 배열은 == 하나 === 하나 같은 비용으로 비교합니다.
[1,2,3]==[1,2,3] 은 false가 되므로
[1,,,2]==[1,undefined,undefuned,2] 는 무조건 false로 나옵니다.
iOS 메모리관리Changwon National University이 자료는 이영록강사님이 2011년 iOS 개발자 포럼에서 발표한 내용을 약간 개선하고 정리한 자료입니다.
iOS의 메모리관리 기법은 retain count라고 하는 독특하면서도 효율적인 방법을 사용하며 최근에 발표된 Xcode에서는 Automatic Reference Counting(ARC) 기법을 통해 release를 사용하에 객체를 소거하는 불편함을 많이 개선하였습니다.
본 자료에서는 이러한 점에 대한 비교적 상세한 설명을 담고 있습니다.
[IT기술칼럼#2] 고급자바스크립트 for AngularJS, React_고급자바스크립트,AngularJS,React전문교육학원탑크리에듀(구로디지털단지역3번출구 2분거리)탑크리에듀(www.topcredu.co.kr), 송석원 교수의 IT기술칼럼#2
자바스크립트는 ES6버전에서 class 키워드를 도입했습니다. 자바스크립트는 객체지향 언어였지만 다른 객체지향언어처럼 사용하기에는 많은 불편함이 있었습니다. 심지어 자바스크립트는 객체지향언어라고 볼 수 없다고 하는 분들도 있었을 정도였죠. 최근에 선풍적인 인기를 얻고 있는 React, Angular 같은 기술은 클래스 문법을 채택했습니다. 따라서, 클래스 문법에 대한 이해가 깊다면 새로운 기술을 습득할 때 많은 도움이 될 것 입니다.
다른 객체지향 언어와 자바스크립트의 차이를 이해하지 못하고 사용하게 되면 많은 문제점이 발생합니다. 안타깝지만 자바스크립트를 사용하고 있는 개발자들 중에서도 제대로 이해하고 사용하시는분은 많지 않은 듯 합니다.
이를 해소하기 위해서 새로 도입한 문법이 class 키워드로 대표되는 클래스 문법입니다. 객체지향 개발방법의 익숙한 개발자들을 위해서 자바스크립트가 새로운 문법을 도입한 것 입니다. 개인적으로 “JavaScript: The Good Parts”의 저자이신 Douglas Crockford 님께서 이를 어떻게 생각하는지 궁금합니다. 자바스크립트를 자바처럼 사용하는 것에 우려를 표하셨었는데, 그 사이 많은 시간이 지났으므로 입장이 바뀌었을지 궁금하군요. 선도적인 개발자들은 ES6에서 도입한 클래스 문법이 좋은 것인가 나쁜 것인가를 갖고 토론을 하기도 합니다.
자바스크립트 클래스의 프로토타입(prototype of class)탑크리에듀(구로디지털단지역3번출구 2분거리)(topcredu.co.kr의 고급자바스크립트강좌)ES5 문법에서는 함수를 만들고 난 후, 추가로 상속용 객체에 함수를 정의했다면 ES6 문법에서는 클래스 범위연산자 안에서 함수를 정의한다는 문법적인 차이만 있을 뿐, 결과적으로 처리되는 방법은 똑같다
2. 자바, C++, C# 등의 class가 없다.
Prototype 기반 언어들에 영감을 받음.
(https://ko.wikipedia.org/wiki/프로토타입_기반_프로
그래밍)
JavaScript는 prototype 객체, 생성자 등을 통해
구현 가능
클래스 기반 vs 프로토타입 기반
3. function Func(x, y) {
this.x = x;
this.y = y;
}
var func = Func(1, 2);
this === ?
생성자 함수와 this
function Func(x, y) {
this.x = x;
this.y = y;
}
var func = new
Func(1, 2);
this === ?
5. When Number is called as a function rather than as a constructor, it
performs a type conversion.
var num = 1;
var num2 = Number(1);
var num3 = new Number(1);
console.log(num === num2);
console.log(num === num3);
console.dir(num);
console.dir(num3);
new 키워드 필수
Number, String 등등에는 안써도 되던데?
Type 변환을 위한 메서드를 추가로 가지고 있을뿐.
new로 생성하지 않으면 instance가 없다.
var func = Func(1, 2);
// func === undefined
Spec.
6. function Person(name) {
this.name = name;
this.getName = function () {
return this.name;
}
this.setName = function (name) {
this.name = name;
}
}
var me = new Person("yb");
console.log(me.getName());
me.setName("new yb");
console.log(me.getName());
공통속성을 추가하는 경우
// Quiz : getName, setName 메서드는
Person에 추가 되었을까? 아니면 me에만 추가
되었을까?
7. 중복되는 영역을 메모리에 올려놓고 사용
-> 메모리 낭비
var personA = new Person("personA");
var personB = new Person("personB");
var personC = new Person("personC");
불필요한 중복
8. personA.getName = function () {
return 'Hello, ' + this.name;
}
console.log(personA.getName());
console.log(personB.getName());
console.log(personC.getName());
변경이 발생했을 때
Hello, personA
personB
personC
변경된 사항이 적용되지 않는다.
9. function Person(name) {
this.name = name;
}
Person.prototype.getName = function () {
return this.name;
}
Person.prototype.setName = function (name) {
this.name = name;
}
var personA = new Person("personA");
var personB = new Person("personB");
var personC = new Person("personC");
console.log(personA.getName());
console.log(personB.getName());
console.log(personC.getName());
공통속성을 추가하는 경우
– Prototype을 이용
Instance마다 개별적으로
가져야 할 프로퍼티
공통으로 가져야 할
프로퍼티
11. 변경이 발생했을 때
Person.prototype.getName = function () {
return 'Hello, ' + this.name;
}
console.log(personA.getName());
console.log(personB.getName());
console.log(personC.getName());
Hello, personA
Hello, personB
Hello, personC
Prototype을 참조하고 있는
다른 모든 Instance에도 모두 적용
12. Prototype의 몇 가지 속성
name : name / hasOwnProperty : true
name : getName / hasOwnProperty : false
name : setName / hasOwnProperty : false
for (var obj in personA) {
console.log('name : ', obj, ' / hasOwnProperty : ' +
personA.hasOwnProperty(obj));
}
- 마치 자신의 속성처럼 열거할 수 있다.(for in 사용)
- hasOwnProperty() 메서드를 사용해 소유여부를 확인
할 수 있다.
13. function Circle(r) {
this.r = r;
}
Circle.prototype.PI = 3.14;
Circle.prototype.getArea = function () {
return 2 * this.r * this.PI;
}
var r2 = new Circle(2);
console.log(r2.getArea());
var r3 = new Circle(3);
console.log(r3.getArea());
var r3Alien = new Circle(3);
r3Alien.PI = 4.74;
console.log(r3Alien.getArea());
내부의 property가 있는 경우에 prototype을 참고하지 않는다.
이런 경우를 다른 property가 가렸다(shadows) 혹은 숨겼다
(hides)라고 한다
- Prototype property가 참조되지 않을 수 있다.
14. Function.prototype.addMethod = function (name, func) {
if(!this.prototype[name]) {
this.prototype[name] = func;
}
}
function Person(name) {
this.name = name;
}
Person.addMethod('setName', function(name) {
this.name = name;
});
Person.addMethod('getName', function () {
return this.name;
});
var personA = new Person("personA");
var personB = new Person("personB");
var personC = new Person("personC");
크락포드옹 스탈
Overriding 을 방지할 수 있다
16. function createObject(parentObj) {
function Func() {}
Func.prototype = parentObj;
return new Func();
}
Object.create() 와 동일
var personPrototype = {
getName : function() {
return this.name;
}
, setName : function (name) {
this.name = name;
}
};
var student = createObject(personPrototype);
console.log(student.getName());
student.setName('student');
console.log(student.getName());
이런 방식으로 prototype을
구현한 것과 같이 생성 가능
Class 기반의 생성자처럼
생성하는 시점에
초깃값을 주고 싶다면?
상속을 구현
17. // 초깃값을 넣고 싶은 경우
var personPrototypeFunc = function(name){
return {
name : name
, getName : function() {
return this.name;
}
, setName : function (name) {
this.name = name;
}
}
};
var student2 =
createObject(personPrototypeFunc('student2'));
console.log(student2.getName());
student2.setName('student2_changed');
console.log(student2.getName());
- 초깃값을 주고 싶은 경우
18. 재정의, 기능확장
function extend(obj, prop) {
if(!prop) {
prop = obj;
obj = this;
}
for (var i in prop) {
obj[i] = prop[i];
}
return obj;
}
var added = {
setAge : function (age) {
this.age = age;
}
, getAge : function () {
return this.age;
}
};
extend(student, added);
student.setAge(25);
console.log(student.getAge());
extend 메서드를 구현하거나
jQuery의 extend 메서드를 사용
19. 슈퍼 클래스와 서브 클래스
function Rectangle(w, h) {
this.width = w;
this.height = h;
}
Rectangle.prototype.area = function () {
return '넓이 : ' + this.width * this.height;
}
// 아래 코드는 Rectangle 클래스를 어떻게 서브 클래스화 하는지 보여준다
function PositionedRectangle(x, y, w, h) {
// 생성자 체이닝
Rectangle.call(this, w, h);
this.x = x; // 사각형의 좌표를 저장한다.
this.y = y;
}
// Rectangle를 서브 클래스화 시키려면 명시적으로 프로토타입 객체를 생성해야 한다.
PositionedRectangle.prototype = new Rectangle();
// PositionedRectangle 객체의 constructor를 가지도록 기본값을 다시 할당한다.
PositionedRectangle.prototype.constructor = PositionedRectangle;
PositionedRectangle.prototype.getPosition = function () {
return 'x : ' + this.x + ' / y : ' + this.y;
}
// 3, 4에 위치한 2x2 사각형
var rect = new PositionedRectangle(3, 4, 2, 2);
console.log(rect.getPosition()); // x : 3 / y : 4
console.log(rect.area()); // 넓이 : 4
// rect 객체는 세 개 클래스의 인스턴스가 된다.
console.log(rect instanceof PositionedRectangle && rect instanceof Rectangle
&& rect instanceof Object); // true
20. 클래스 방식의 상속패턴 완성
// 라이브러리로
var inherit = function(Parent, Child) {
var F = function() {};
return function(Parent, Child) {
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.super = Parent.prototype;
return new Child();
}
}();
var student2 = inherit(Person, Student);
console.dir(student2);
21. 생성자 체이닝
function PositionedRectangle(x, y, w, h) {
this.superclass(w, h);
this.x = x;
this.y = y;
}
// 상위 클래스 생성자에 대한 참조를 저장한다.
PositionedRectangle.prototype.superclass = Rectangle;
처음의 예제처럼 call이나 apply를 사용하지 않아도 된다.
생성자 함수에서 상위 클래스의 생성자 함수를 명시적으로
호출할때 아래와 같은 방법도 가능
22. 재정의 된 메서드 호출하기
서브 클래스에서 재정의하는 메서드는 종종 기존 메서드에 있던
기능을 완전히 교체하기보다 확장시킨다.
// 메서드 재정의
Rectangle.prototype.toString = function () {
return '[' + this.width + ',' + this.height + ']';
}
PositionedRectangle.prototype.toString = function () {
return '(' + this.x + ',' + this.y + ')' + // PositionedRectangle 필드들
Rectangle.prototype.toString.apply(this); // 상위 클래스에 체이닝. 어떤 객체를
참조할지 지정하기 위해 apply()와 함께 호출.
};
console.log(rect.toString());
23. 재정의 된 메서드 호출하기
서브 클래스에서 재정의하는 메서드는 종종 기존 메서드에 있던
기능을 완전히 교체하기보다 확장시킨다.
// 메서드 재정의
Rectangle.prototype.toString = function () {
return '[' + this.width + ',' + this.height + ']';
}
PositionedRectangle.prototype.toString = function () {
return '(' + this.x + ',' + this.y + ')' + // PositionedRectangle 필드들
Rectangle.prototype.toString.apply(this); // 상위 클래스에 체이닝. 어떤 객체를
참조할지 지정하기 위해 apply()와 함께 호출.
};
console.log(rect.toString());
24. Functional Programming
함수의 조합으로 작업을 수행한다.
작업에 필요한 데이터와 상태는 변하지 않는다.
오로지 함수만 변한다.
명령형 프로그래밍(Imperative Programming)
<-> 함수형 프로그래밍(Functional Programming)
명령을 순서대로 기술하는 것은 겉모양이 함수여도 Procedure에
가깝다.
함수형 프로그래밍의 함수는 독립적으로 작동하는 순수함수(Pure
function)
25. 명령형 프로그래밍
function sum(arr) {
var len = arr.length;
var i = 0, sum = 0;
for (; i < len; i++) {
sum += arr[i];
}
return sum;
}
var arr = [1, 2, 3, 4];
console.log(sum(arr));
function multiply(arr) {
var len = arr.length;
var i = 0, sum = 1;
for (; i < len; i++) {
sum *= arr[i];
}
return sum;
}
var arr = [1, 2, 3, 4];
console.log(multiply(arr));
유사한 로직이지만 매번 새로 구현해야 한다.
26. 함수형 프로그래밍
function reduce(func, arr, memo) {
var len = arr.length, i= 0, accum = memo;
for (; i < len; i++) {
accum = func(accum, arr[i]);
}
return accum;
}
var arr = [1, 2, 3, 4];
var sum = function(x, y) {
return x+y;
}
var multiply = function(x, y) {
return x*y;
}
console.log(reduce(sum, arr, 0));
console.log(reduce(multiply, arr, 1));
추상화, 모듈화를 통한
재사용이 가능하다.
27. 핵심요소
함수 = 일급객체
값으로 사용 가능하고 전달할 수 있다.
클로저
=> 함수의 실행이 끝난 후에도 존재하고, 모듈
화를 가능하게 해준다.
28. 함수 실행 소요시간
function getCostTime(func, param, repeatTime) {
var start = new Date().getTime();
if(repeatTime) {
for (var i = 0; i < repeatTime; i++) {
func(param);
}
} else {
func(param);
}
var elapsed = new Date().getTime() - start;
console.log('소요시간 : ' + elapsed + ' ms');
return elapsed;
}
getCostTime(function(){
for (var i = 0; i < 1000000000; i++) {
}
});
29. Memoization
var fact = function () {
var cache = {'0' : 1};
var func = function (n) {
var result = 0;
if(typeof(cache[n]) === 'number') {
result = cache[n];
} else {
result = cache[n] = n * func(n-1);
}
console.log(n + '! = '+ result);
return result;
}
return func;
}();
console.log(fact(10));
console.log(fact(20));
31. 피보나치 수열
var fibo = function() {
var cache = {'0' : 0, '1' : 1};
var func = function (n) {
if(typeof(cache[n]) === 'number') {
result = cache[n];
} else {
result = cache[n] = func(n-1) + func(n-2);
}
return result;
};
return func;
}();
console.log(fibo(10));
32. 피보나치 수열
var cacher = function (cache, func) {
var calculate = function (n) {
if(typeof(cache[n]) === 'number') {
result = cache[n];
} else {
result = cache[n] = func(calculate, n);
}
return result;
}
return calculate;
};
var fact = cacher({'0' : 1}, function (func, n) {
return n*func(n-1);
});
var fibo = cacher({'0' : 0, '1' : 1}, function (func, n) {
return func(n-1) + func(n-2);
});
//console.log(fact(10));
console.log(fibo(5));
33. Curry – 새로운 함수 정의
function calculate(a, b, c) {
return a * b + c;
}
function curry(func) {
var args = Array.prototype.slice.call(arguments, 1);
return function () {
return func.apply(null, args.concat(Array.prototype.slice.call(arguments)));
}
}
var new_func1 = curry(calculate, 1);
console.log(new_func1(2, 3));
var new_func2 = curry(calculate, 1, 3);
console.log(new_func2(3));
34. Bind - 새로운 함수 정의
var print_all = function (arg) {
for (var i in this) { console.log(i + " : " + this[i]); }
for (var j in arguments) { console.log(j + " : " +
arguments[j]); }
}
var myobj = {name: "zzoon"};
var myfunc = print_all.bind(myobj);
myfunc();
var myfunc1 = print_all.bind(myobj, 'yb', 'others');
myfunc1('insidejs');
35. Wrapper 함수 구현
function wrap(object, method, wrapper) {
var fn = object[method];
return object[method] = function () {
return wrapper.apply(this, [fn].concat(Array.prototype.slice.call(arguments)));
};
}
Function.prototype.original = function (value) {
this.value = value;
console.log("value : " + this.value);
}
var Mywrap = wrap(Function.prototype, "original", function (orig_func, value) {
this.value = 20;
orig_func(value);
console.log("wrapper value : " + this.value);
});
var obj = new Mywrap('yb');
36. each 함수 구현
function each(obj, fn, args) {
if(obj.length == undefined) {
for (var i in obj) {
fn.apply(obj[i], args || [i, obj[i]]);
}
} else {
for (var i = 0; i < obj.length; i++) {
fn.apply(obj[i], args || [i, obj[i]]);
}
}
return obj;
}
each([1, 2, 3], function (idx, num) {
console.log(idx + " : " + num);
});
37. map 함수 구현
Array.prototype.map = function (callback) {
var obj = this;
var value, mapped_value;
var A = new Array(obj.length);
for (var i = 0; i < obj.length; i++) {
value = obj[i];
mapped_value = callback.call(null, value);
A[i] = mapped_value;
}
return A;
};
var arr = [1, 2, 3];
var new_arr = arr.map(function (value) {
return value * value;
});
console.log(new_arr);
38. reduce 함수 구현
Array.prototype.reduce = function (callback, memo) {
var obj = this;
var value, accumulated_value = 0;
for (var i = 0; i < obj.length; i++) {
value = obj[i];
accumulated_value = callback.call(null, accumulated_value, value);
}
return accumulated_value;
};
var arr = [1, 2, 3];
var accumulated_val = arr.reduce(function (a, b) {
return a + b;
})
console.log(accumulated_val);