[ydkjsy]Get Started-A-Exploring Further

p*0*q·2021년 1월 2일
0

YDKJS

목록 보기
4/16

Values vs. References

복사 시, primitives는 값만 복사, object는 참조 복사. 즉, value 유형에 따라 복사도 다르다.

So Many Function Forms

var awesomeFunction = function(coolThings) {//이름이 없어 익명 함수라고 한다.
    // ..
    return amazingStuff;
};
awesomeFunction.name;
// "awesomeFunction"
//name inference 이름 추론.
// |
// V
// let awesomeFunction = ..
// const awesomeFunction = ..
var awesomeFunction = function someName(coolThings) {
    // ..
  //컴파일 시 someName. 런타임 시에 해당 줄 처리할 때 식별자 awesomeFunction 생김.
    return amazingStuff;
};

awesomeFunction.name;
// "someName"

보통 함수 이름 조사 및 에러 스택 추적을 보고시 사용된다.

이름 추론의 경우 = 함수 표현식일 경우에만 이름이 생성된다. 이름이 추론될 뿐, 식별자를 갖는 것은 아니다. 그래서 재귀, 이벤트 언바인딩에서 사용할 이름이 없다. 또한 argument로 넘겨질 경우에도 그냥 익명함수다.
목적이 없는 함수는 없다. 목적에 맞게 이름을 쓰도록 해서 가독성을 높이고 코드를 읽는데 불필요하게 쓰는 시간을 줄이자.

다양한 함수 선언 종류. method들도 함수랑 다를 바 없다.

//function declaration
// generator function declaration
function *two() { .. }

// async function declaration
async function three() { .. }

// async generator function declaration
async function *four() { .. }

// named function export declaration (ES6 modules)
export function five() { .. }


//function expression
// IIFE
(function(){ .. })();
(function namedIIFE(){ .. })();

// asynchronous IIFE
(async function(){ .. })();
(async function namedAIIFE(){ .. })();

// arrow function expressions
var f;
f = () => 42;
f = x => x * 2;
f = (x) => x * 2;
f = (x,y) => x * y;
f = x => ({ x: x * 2 });
f = x => { return x * 2; };
f = async x => {
    var y = await doSomethingAsync(x);
    return y * 2;
};
someOperation( x => x * 2 );
// ..

Coercive Conditional Comparison

주의.

var x = "hello";

if (x) {
    // will run!
}

if (x == true) {
    // won't run :(
}

if (Boolean(x) == true) {
    // will run
}

// which is the same as:

if (Boolean(x) === true) {
    // will run
}

Prototypal "Classes"

흠.. 사실 class가 클래스 지향 설계 패턴으로 접하해 이를 이용하는 것을 추천.
그래도.

var Classroom = {
    welcome() {
        console.log("Welcome, students!");
    }
};

var mathClass = Object.create(Classroom);
//프로토타입을 통해 Classroom 객체에 mathClass 연결.
//위임
mathClass.welcome();
// Welcome, students!
function Classroom() {
    // ..
}

Classroom.prototype.welcome = function hello() {
  //모든 함수는 기본적으로 함수의 프로토타입이 아닌 prototype의 이름의 빈 객체가 참조된다.
  //new로 함수를 호출하면 이 객체로 링
    console.log("Welcome, students!");
};

var mathClass = new Classroom();
//위임이 아닌 상속으로 정의됐다.
mathClass.welcome();
// Welcome, students!

0개의 댓글

관련 채용 정보