Javascript Function
2020. 06. 13
Jaewoo KIM
๐โโ๏ธ Function Instance
Function Instance ๊ธฐ์ค
Function ๊ตฌ๋ถ
- Built In Function Object
- Funtion Object : function ํค์๋๋ก ์์ฑ
- Funtion Instance: new ์ฐ์ฐ์๋ก ์์ฑ
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
var person = new Persion('Jaewoo KIM')
person.getName();
Function Instance ์์ฑ
1. function Person(name) {...}
2. Person.prototype.getName = function() {}
3. var person = new Person('Jaewoo KIM')
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
var obj = new Person('Jaewoo KIM')
log(obj.name);
log(obj.getName());
๐โโ๏ธ ์์ฑ์ ํจ์
์์ฑ์ ํจ์
- new ์ฐ์ฐ์์ ํจ๊ป ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ํจ์
- new Person() ์์ Person() ์ด ์์ฑ์ ํจ์
- new ์ฐ์ฐ์
- ์ธ์คํด์ค ์์ฑ์ ์ ์ด
- ์์ฑ์ ํจ์ ํธ์ถ
- ์์ฑ์ ํจ์
- ์ธ์คํด์ค ์์ฑ, ๋ฐํ
- ์ธ์คํด์ค์ ์ด๊น๊ฐ ์ค์
์์ฑ์ ํจ์ ์คํ ๊ณผ์
- ์์ง์ด new ์ฐ์ฐ์๋ฅผ ๋ง๋๋ฉด
function์ [[Construct]]๋ฅผ ํธ์ถ - fuction ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ ๋
Person() ํจ์ ์ ์ฒด๋ฅผ [[Constuct]]์ ์ค์ - [[Construct]]์์ ์ธ์คํด์ค๋ฅผ ์์ฑํ์ฌ ๋ฐํ
- ๋ฐํ๋ ์ธ์คํด์ค๋ฅผ new ์ฐ์ฐ์๊ฐ ๋ฐ์
new ์ฐ์ฐ์๋ฅผ ํธ์ถํ ๊ณณ์ผ๋ก ๋ฐํ
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
var obj = new Person('Jaewoo KIM')
new ์ฐ์ฐ์๊ฐ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ ๊ฒ์ผ๋ก ์๊ฐํ ์ ์์ง๋ง ์ค์ ๋ก function์ [[Construct]]๊ฐ ์ธ์คํด์ค๋ฅผ ์์ฑ
์ธ์คํด์ค ์์ฑ ๊ณผ์
- new Person() ์คํ
- Person ์ค๋ธ์ ํธ์ [[Construct]] ํธ์ถ
- ํ๋ผ๋ฏธํฐ๋ฅผ [[Construct]]๋ก ์ ๋ฌ
- ๋น Object ์์ฑ
- ์ฌ๊ธฐ์ ์ค๋ธ์ ํธ๋ฅผ ํ๋์ฉ ์ฑ์๋๊ฐ
- Object ๋ด๋ถ ์ฒ๋ฆฌ์ฉ ํ๋กํผํฐ๋ฅผ ์ค์
- Object์ [[Class]]์ "Object" ์ค์
- Person.prototype์ ์ฐ๊ฒฐ๋ ๋ฉ์๋๋ฅผ
์์ฑํ ์ธ์คํด์ค์ [[Prototype]]์ ์ค์
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
var obj = new Person('Jaewoo KIM')
Person ์ธ์คํด์ค: {
name: 'Jaewoo KIM'
__proto__ = {
constructor: Person,
getName: fuction() {},
__proto__: Object
}
}
๐โโ๏ธ constructor ํ๋กํผํฐ
Constructor ํ๋กํผํฐ
- ์์ฑํ๋ function ์ค๋ธ์ ํธ๋ฅผ ์ฐธ์กฐ
- function ์ค๋ธ์ ํธ๋ฅผ ์์ฑํ ๋ ์ค์
- prototype์ ์ฐ๊ฒฐ๋์ด ์์
- ES5: constructor ๋ณ๊ฒฝ ๋ถ๊ฐ
- ์์ฑ์ ํ์ฉ ๋ถ๊ฐ
- ES6: constructor ๋ณ๊ฒฝ ๊ฐ๋ฅ
- ํ์ฉ์ฑ ๋์
Person fuction ์ค๋ธ์ ํธ: {
prototype: {
constructor: Person
}
}
Constructor ๋น๊ต
- Person === Person.prototype.constructor
- Person === obj.constructor
- typeof Person
- typeof obj
- function ์ค๋ธ์ ํธ๋ฅผ ์ธ์คํด์ค๋ก ์์ฑํ๋๋
ํ์ ์ด ๋ณ๊ฒฝ๋์์- [[Construct]]๊ฐ ์คํ๋ ๋ ์์ฑ๋ ์ค๋ธ์ ํธ์ [[Class]]์ 'Object'๋ฅผ ์ค์ ํ๊ธฐ ๋๋ฌธ
var Person = function(){};
var result =
Person === Person.prototype.constructor;
log(result); // true
var obj = new Person();
log(Person === obj.contructor) // true
log(typeof Person) // function
log(typeof obj) // object
๐โโ๏ธ prototype, ์์
prototype ์ค๋ธ์ ํธ ๋ชฉ์
- prototype ํ์ฅ
- prototype์ ํ๋กํผํฐ๋ฅผ ์ฐ๊ฒฐํ์ฌ ํ์ฅ
- Person.prototype.getName = function() {}
- ํ๋กํผํฐ ๊ณต์
- ์์ฑํ ์ธ์คํด์ค์์ ์๋ณธ prototype์ ํ๋กํผํฐ ๊ณต์
- ์ธ์คํด์ค ์์
- function ์ธ์คํด์ค๋ฅผ ์ฐ๊ฒฐํ์ฌ ์์
- Person.prototype = new Point()
์ธ์คํด์ค ์์
- ์ธ์คํด์ค ์์ ๋ฐฉ๋ฒ
- prototype์ ์ฐ๊ฒฐ๋ ํ๋กํผํฐ๋ก ์ธ์คํด์ค ์์ฑํ์ฌ ์์๋ฐ์ prototype์ ์ฐ๊ฒฐ
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
function Point(name) {
Person.call(this, name);
}
Point.prototype =
Object.create(Person.prototype, {});
var obj = new Point('Jaewoo KIM')
log(obj.getName())
์ธ์คํด์ค ์์
- ์ธ์คํด์ค ์์ ๋ฐฉ๋ฒ
- ES6์ Class ์์
class Person {
constructor(name) {
this.name = name;
}
getName() {
return this.name;
}
}
class Point extends Person {
constructor(name) {
super(name)
}
}
const obj = new Point('Jaewoo KIM')
log(obj.getName())
๐โโ๏ธ prototype ํ์ฅ
prototype ํ์ฅ ๋ฐฉ๋ฒ
- prototype์ ํ๋กํผํฐ๋ฅผ ์ฐ๊ฒฐํ์ฌ ์์ฑ
- prototype.name = value ํํ
- ์ผ๋ฐ์ ์ผ๋ก function์ ์ฌ์ฉ
- prototype์ null์ ์ค์ ํ๋ฉด ํ์ฅ ๋ถ๊ฐ
ํ๋กํผํฐ ์ฐ๊ฒฐ ๊ณ ๋ ค์ฌํญ
- prototype์ ์ฐ๊ฒฐํ ํ๋กํผํฐ๊ฐ ๋ง์ ๋
- Person.prototype.nam1 ... ~ N ํํ๋
๋ฐ๋ณตํด์ผ ํด์ ์์ฑํด์ผ ๋๊ธฐ ๋๋ฌธ์ ๋ฒ๊ฑฐ๋ก์ - Person.prototype = {name1: value, .... N}
- Person.prototype.nam1 ... ~ N ํํ๋
- constructor๊ฐ ์ง์์ง๋ ๋ฌธ์ ์ ๋์ฑ
- {name1: value, ...} ํํ๋ก ์ค์ ํ ํ
prototype์ constructor๋ฅผ ๋ค์ ์ฐ๊ฒฐ
- {name1: value, ...} ํํ๋ก ์ค์ ํ ํ
constructor ์ฐ๊ฒฐ
- ์ค๋ธ์ ํธ ๋ฆฌํฐ๋ด {} ์ ์ฌ์ฉํ์ฌ ํ๋กํผํฐ๋ฅผ ์ฐ๊ฒฐํ ๋
constructor๊ฐ ์ง์์ง๋ ๊ฒ์ ๊ณ ๋ คํด์ผ ํ๋ค. - constructor๊ฐ ์์ด๋ ์ธ์คํด์ค๊ฐ ์์ฑ๋์ง๋ง
- constructor๊ฐ ์ฐ๊ฒฐ๋ ๊ฒ์ด ์ ์์ด๋ฏ๋ก...
function Person() {};
Person.prototype = {
constructor: Person,
setName: function() {}
}
var obj = new Person();
log(obj.constructor);
// function Person() {}
prototype ํ์ฅ๊ณผ ์ธ์คํด์ค ํํ
- fuction Person() {};
- Person ์ค๋ธ์ ํธ ์์ฑ
- Person.prototype.getName
ย ย ย ย ย ย ย ย ย ย ย ย ย ย = function(){}- getName ๋ฉ์๋ ์ฐ๊ฒฐ
- var obj = new Person();
- ์ธ์คํด์ค ์์ฑ ๋ฐ ํ ๋น
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
var obj = new Person('Jaewoo KIM');
obj.getName();
obj: {
name: 'Jaewoo KIM'
__proto__ = {
constructor: Person,
getName: fuction() {},
__proto__: Object
}
}
๐โโ๏ธ this์ prototype
this๋ก ์ธ์คํด์ค ์ฐธ์กฐ
- this๋ก ๋ฉ์๋๋ฅผ ํธ์ถํ ์ธ์คํด์ค ์ฐธ์กฐ
- var obj = new Person();
- obj.get() ํํ์์ this๋ก obj ์ฐธ์กฐ
- ์ธ์คํด์ค์์ ๋ฉ์๋ ํธ์ถ ๋ฐฉ๋ฒ
- prototype์ ์ฐ๊ฒฐ๋ ํ๋กํผํฐ๊ฐ __proto__์ ์ค์ ๋๊ณ
์ธ์คํด์ค ํ๋กํผํฐ๊ฐ ๋๋ค
- prototype์ ์ฐ๊ฒฐ๋ ํ๋กํผํฐ๊ฐ __proto__์ ์ค์ ๋๊ณ
this์ prototype
- log("1:",ย this.name);
- ์ฌ๊ธฐ์ this๋ ์์ฑ ์ธ์คํด์ค ์ฐธ์กฐ
- name ํ๋กํผํฐ๊ฐ ์๋๋ผ๊ณ ์๋ฌ๊ฐ
๋ฐ์ํ์ง ์๊ณ undefined๋ฅผ ๋ฐํ
- obj.getName();
- ์ฌ๊ธฐ์ this๋ ํธ์ถํ ์ธ์คํด์ค
- this.setPoint()
- ์ฌ๊ธฐ์ this๋ ์์ฑ ์ธ์คํด์ค ์ฐธ์กฐ
function Person() {
log("1:". this.name)
};
Person.prototype.getName = function() {
this.setName();
log('2:'. this.name)
}
Person.prototype.setName = function() {
this.name = "Jaewoo KIM"
}
var obj = new Person('Jaewoo KIM');
obj.getName();
prototype ๋ฉ์๋ ์ง์ ํธ์ถ
- Person.prototype.getName();
- ์ธ์คํด์ค๋ฅผ ์์ฑํ์ง ์๊ณ ์ง์ ํธ์ถ
- Person.prototype์ this๋ก ์ฐธ์กฐ
- obj ์ธ์คํด์ค์๋ name์ด ์์ง๋ง
Person.prototype์๋ ์์
function Person(name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
}
var obj = new Person('Jaewoo KIM');
log(obj.getName()); // Jaewoo KIM
log(Person.prototype.getName()) // undefined
๐โโ๏ธ prototype ํ๋กํผํฐ ๊ณต์ ์์
ํ๋กํผํฐ ๊ณต์ ์์
- ์ฌ์ฉํ๋ ์์ ์ prototype์ ํ๋กํผํฐ ๊ณต์
- prototype์ ํ๋กํผํฐ๋ก ์ธ์คํด์ค๋ฅผ ์์ฑํ์ง๋ง
- ์ธ์คํด์ค์ ํ๋กํผํฐ๋ ์๋ณธ prototype์ ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐ
- ๋ณต์ฌ๊ฐ๋ ์ด ์๋
- ์ธ์คํฐ์ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ฉด ์๋ณธ prototype์ ๋ฉ์๋๋ฅผ ํธ์ถ
- ์๋ณธ prototype์ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ๋ฉด ๋ชจ๋ ์ธ์คํด์ค์์ ์ฌ์ฉ๊ฐ๋ฅ
ํ๋กํผํฐ ๊ณต์ ์์
function Person() {
this.name = 'Jaewoo KIM';
};
var obj = new Person();
log(obj.getName); // undefined
Person.prototype.getName = function() {
return this.name
}
var result = obj.getName()
log(result) // Jaewoo KIM
๐โโ๏ธ ์ธ์คํด์ค ํ๋กํผํฐ
์ธ์คํด์ค ํ๋กํผํฐ
- prototype์ ์ฐ๊ฒฐ๋ ํ๋กํผํฐ๋ ์ธ์คํด์ค ํ๋กํผํฐ
- ์ง์ ์ธ์คํด์ค์ ์ฐ๊ฒฐ๋ ํ๋กํผํฐ์ ์ฐจ์ด ์์
- ์ธ์คํด์ค์ ํ๋กํผํฐ๋ฅผ prototype์ผ๋ก ๋ง๋
์ธ์คํด์ค ํ๋กํผํฐ ๋ณด๋ค ๋จผ์ ์ฌ์ฉ - ์ธ์คํด์ค๋ง๋ค ๊ฐ์ ๋ค๋ฅด๊ฒ ๊ฐ์ง ์ ์์
- ์ธ์คํด์ค๋ฅผ ์ฌ์ฉํ๋ ์ค์ํ ๋ชฉ์
obj ์ธ์คํด์ค = {
name: 'Jaewoo KIM',
getName: function() {},
__proto__: {
getName: fuction() {}
}
}
์ธ์คํด์ค ํ๋กํผํฐ ์ฐ์ ์ฌ์ฉ
function Person(name) {
this.name = name;
};
Person.prototype.getName = function() {
return 'Jaewoo KIM'
}
var obj = new Person('sat10am');
obj.getName = function () {
return this.name
}
log(obj.getName());
obj ์ธ์คํด์ค = {
name: 'sat10am',
getName: function() {},
__proto__: {
getName: fuction() {}
}
}
๋
sat10am-200613
By Jaewoo KIM
sat10am-200613
- 315