📁
til
  • TIL(Today I Learned)
  • javascript
    • value-number-string-boolean-null-undefined
    • primitive-reference
    • Hoisting
    • Prototypes in Javascript
    • this
    • prototype
    • 콜백함수 (Callback function)
    • 함수의 호출
    • 자료구조 new keyword
    • closure
    • Promise
    • event-loop
    • array-object
    • 객체
    • Arguments
    • higher order function
    • operators-function-control-flow
    • 객체 생성 패턴 3가지
    • Javascript scopes
    • Functional Programming
    • Design Patterns
    • 데이터 타입
    • Object 객체
    • 표준 내장 객체의 확장
    • 참조
    • 함수
    • 상속(Inheritance)
    • this - 'this'를 사용하는 해당 함수를 '어떻게' 실행하느냐에 따른 4가지 this 정의
    • 전역객체(Global object)
    • 객체 지향 프로그래밍
    • The 'new' keyword - Object Creation in JavaScript
  • javascript-api
    • Number
      • Number.MAX_VALUE
      • Number.isInteger
      • Number.NEGATIVE_INFINITY
      • Number.isNaN()
      • Number.POSITIVE_INFINITY
      • Number.parseFloat
      • Number.EPSILON
      • number.toExponential
      • Number.MAX_SAFE_INTEGER
      • Number1 - 자연수, 정수, 10진수, 2진수, 부동소수점, 실수
      • Number.isSafeInteger()
      • Number.MIN_VALUE
      • Number.parseInt
      • Number.NaN
      • Number.isFinite()
      • Number.MIN_SAFE_INTEGER
      • toFixed
    • string.split
    • String.fromCodePoint
    • string.trimEnd
    • string.padStart
    • string.@@iterator
    • String.fromCharCode
    • string.toUpperCase
    • string.codePointAt
    • string.toLowerCase()
    • string.toString
    • string.includes
    • string.replace()
    • string.charAt
    • String.lastIndexOf
    • string.slice
    • string.search
    • string.padEnd
    • string.substring
    • string.length
    • string.trim
    • string.localeCompare
    • String.indexOf
    • string.endsWith
    • string.valueOf
    • String.raw
    • string.matchAll()
    • string.repeat
    • string.match
    • String.prototype
    • string.startsWith
    • string.charCodeAt
    • string.trimStart
    • string.concat
    • string.toLocaleUpperCase()
    • string.toLocaleLowerCase
    • String
  • learn-node
    • debugger
    • Tip
  • schema-design
    • Database Schema Design
    • Database Schema Design
  • react
    • LifeCycle
    • redux
    • Context API
    • 함수형 컴포넌트와 클래스, 어떤 차이가 존재할까?
    • Hooks과 useEffect 사용해 보기
    • Route
    • async wait 사용하기
    • Hooks API Reference
    • context
    • npm uninstall 하는법
    • test 만들기
  • tip
    • 클린코드
    • BxSlider로 텍스트 흐르는 효과 만들기
  • javascript30
    • Event Capture, Propagation, Bubbling and Once
    • Object and Arrays - Reference VS Copy
  • typescript
    • 우아한 타입스크립트 2부
    • The Basic Cheatsheet
    • TypeScript
    • Type Guards and Differnetiating Types
    • 우아한 타입스크립트
    • Generics
  • git-from-the-hell
    • git
    • init-status-add-commit-log-stage-repository
    • log-diff
    • 머지 후 branch 삭제하기
    • 지옥에서 온 Git
    • reset-revert
    • develop branch 를 pull 하고 싶을때
  • conference-and-seminar
    • 모던 프론트엔드 개발환경의 이해
  • fire-base
    • Firebase .gitignore
  • vanillacoding
    • Data Structures
    • Sorting Algorithms - part 1
    • Promise
    • class
    • 04.quiz
    • 07.event-loop
    • Design Patterns
    • OOP (Object Oriented Programming)
  • etc
    • 알고리즘 코드리뷰
    • 스스로 공부하는 법
    • 바닐라코딩 수강 후기
    • async 과제 후기 - 비동기, 동기, 클로저, 배열과 객체
    • 유용한 사이트
  • algorithm
    • The Supermarket Queue
    • Find the odd int
    • The Office III - Broken Photocopier
    • Directions Reduction
    • The Office II - Boredom Score
    • Divisible Sum Pairs
    • Codewars 이용자 솔루션 모음
    • Shortest Word
    • find key
    • Two Sum
    • Simple Pig Latin
  • Book
    • the essence of object-orientation
      • 타입과 추상화
      • 객체 지도
      • 이상한 나라의 객체
      • 추상화 기법
      • 05. 책임과 메시지
      • 07.함께 모으기
      • 04. 역할, 책임, 협력
      • 협력하는 객체들의 공동체
  • ecma-script2015
    • Object Literal Upgrades
    • default-parameter-template-literals-arrow-functions
    • spread-operator-rest-param
    • let-const-rest-parameter-spread-operator-destructuring
  • http
    • 웹 브라우저에 URL을 입력했을 때 어떻게 웹 페이지가 보여질까?
  • jest
    • toThrow(error?)
  • data-structures
    • Data Structures
  • express
    • express generator
    • CORS
  • css-flexible-box-layout
    • flex 해버렸지 뭐야
  • git
    • Git
  • mongodb
    • MongoDB
  • markdown
    • use-markdown
  • cmder
    • cmd 명령어 모음
  • debug
    • trackClicksEx function error
  • npm
    • NPM TOKEN 설정하기
  • storybook
    • Storybook
  • sort
    • Sorting Algorithms - part 1
  • javascript-koans
    • Javascript Koans 오답노트
  • rxjs
    • Rx.js
  • dom-elements
    • HTML Element
  • redux-toolkit
    • Redux Toolkit
  • github-actions
    • GitHub Actions
  • redux-middleware
    • redux middleware
  • rest
    • rest
  • css-rendering
    • 코드 스피츠 - CSS Rendering 1회차 2/2
    • 코드 스피츠 - CSS Rendering 1회차 1/2
  • you-dont-know-js
    • 타입
  • server
    • # shutdown local server
  • semantic-versioning
    • Semantic Versioning 2.0.0
Powered by GitBook
On this page
  • 일반함수 실행(Regular function call)
  • Dot Notation
  • Function.prototype.call, Function.prototype.apply, Function.prototype.bind
  • 'new' keyword: Constructor function (생성자 함수)

Was this helpful?

  1. javascript

this - 'this'를 사용하는 해당 함수를 '어떻게' 실행하느냐에 따른 4가지 this 정의

this는 무조건 객체다.

  1. 일반함수 실행

    strict 모드에서는 일반함수 호출일때 this값이 바뀐다.

    window라고 직접적으로 지정하지 않을때 error가 뜬다. window를 굳이 사용하는 일이 적고 헷갈리게 만드는일이기 때문이다.

  2. dot notation 점방식

  3. call, aply, bind로 호출

  4. new로 호출

일반함수 실행(Regular function call)

example 1

var name = sla;

function log () {
    console.log(this.name); 
}

log();

example 2 - strict mode

'use strict';
var name = 'sla';

function log () {
    console.log(this.name); //'this' === undefined
}

log();

strict mode에 대해 찾아보자. window라고 지정해야한다. window를 굳이 this로 쓰지 않기 때문이다.

example 3

var age = 100;

function foo () {
    var age = 99;
    bar(age);
}

function bar (age) {
    console.log(this.age);//100
}

foo();

bar는 일반호출 방식으로 호출했기 때문에 100이 찍힌다.

Dot Notation

example 1

var age = 100;

var sla = {
    age: 28,
    foo: function foo () {
        console.log(this.age);
    }
};

sla.foo();

점 앞에 찍힌게 this가 된다.

example 2

var age = 100;
var sla = {
    age: 35,
    foo: function() {
        console.log(this.age);
    }
}
var yami = {
    age: 7,
    foo: sla.foo
}
sla.foo(); // 35
yami.foo(); // 7
foo(); // foo() is not defined

Function.prototype.call, Function.prototype.apply, Function.prototype.bind

call - 인자로 여러개를 받을 수 있고, 첫번째는 무조건 this apply - 인자는 두개만 받을 수 있고, 첫번째는 무조건 this bind - 변수에 할당후 사용해야한다.

example - call, apply

var age = 100;

function foo (a, b, c) {
    console.log(this.age);
    console.log(arguments);
}

var sla = {
    age: 28
};

foo.call(sla, 1, 2, 3, 4, 5, 6, 7, 8, 9);
foo.apply(sla, [ 1, 2, 3, 4, 5, 6, 7, 8, 9]);

example - bind

var age = 100;

function foo () {
    console.log(this.age); //28
    console.log(arguments); //1, 2, 3, 1, 2, 3, 4, 5
}

var sla = {
    age: 28
};

var bar = foo.bind(sla, 1, 2, 3);
bar(1, 2, 3, 4, 5);

'new' keyword: Constructor function (생성자 함수)

example 1

function Person () {
    console.log(this);
}

new Person();

new를 붙이고 함수를 실행했을 때는 아주 깨끗한 빈 객체가 return 된다. {}

example 2

function Person () {
    this.name = 'sla';
    console.log(this);
}

var sla = new Person();
console.log(sla);

this.프로퍼티 , this.메소드를 리턴해준다. 따로 return을 안써도 된다.

설명

function Person () {
    // this = {}; //빈 객체를 만든다.
    this.name = 'sla';
    // this = {name: 'sla' }; this의 빈 객체는 내가 만든 프로퍼티,메소드가 담긴다.
    // return this; 따로 return을 쓰지 않아도 return 된다.
}

var sla = new Person();
console.log(sla);

생성자함수에서 객체를 return할땐 return 된다

function foo() {
    var age = 100;
    return 100;
}

new foo(); //{}

생성자 함수로 만들었을 때 일반적으로 {}빈 객체나 {}안에 this만 담겨 리턴한다.

하지만 객체를 리턴할 때는 return이 사용가능하다.

function foo () {
    var age = 100;
    return { hahaha: 23232 };
}

new foo() //{hahaha: 23232 };

Quiz 1

var age = 100;

const something = {
  age: 1,
  speak: function () {
    console.log(this.age); // 100
  }
};

const butler = {
  age: 10,
  serve: function (cb) {
    cb();
  }
};

butler.serve(something.speak);

Quiz 2

function programmer() {
  this.isSmart = false;
  this.upgrade = function (version) {
    this.isSmart = !!version;
    work();
  };
}

function work() {
  if (this.isSmart) {
    window.alert("I can do my work! I am smart!"); // ?
  }
}

var programmer = new programmer();
// THINK: What should happen?
programmer.upgrade(1.1);
Previous상속(Inheritance)Next전역객체(Global object)

Last updated 5 years ago

Was this helpful?