1. ๋ฉ์๋ ์ ์
const obj = {
foo() {
return 'foo';
}
bar: function() {
return 'bar';
}
};
console.log(obj.foo());
// expected output: "foo"
2. ๋ฉ์๋ ํธ์ถ ํํ
1) OOP์ ์ผ๋ฐ์ ์ธ ๋ฉ์๋ ํธ์ถ ํํ
var data = ["book", "์ฑ
", "123"]
var obj = new Array();
var result = obj.concat(data); //concat ๋ฉ์๋ ํธ์ถ
2) JS์ ๋ฉ์๋ ํธ์ถ ํํ
- ๋ฐ์ดํฐ๋ก ๋ฉ์๋ ํธ์ถ
var data = ["book", "์ฑ
", "123"]
var result = data.concat(); //Array์ concat ๋ฉ์๋ ํธ์ถ
data๊ฐ ๋ฐฐ์ด์ด๋ฏ๋ก Array์ concat() ๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ , data๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋๊ฒจ์ค๋ค.
- ์ค๋ธ์ ํธ์ ํจ์ ํธ์ถ
var data = ["book", "์ฑ
", "123"]
var bookObj = {
concat : function(data){
return data.concat();
}
};
log(bookObj.concat(data));
์ค๋ธ์ ํธ๋ฅผ ํจ์๋ฅผ ํธ์ถํ๊ฒ ๋๋ฉด ์ธ์คํด์ค๋ฅผ ์๋ก ์์ฑํ ํ์๊ฐ ์์ด ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ฐจ์ง๋ฅผ ์ ๊ฒํ๋ค๋ ์ฅ์ ์ด ์๋ค.
- ์ธ์คํด์ค์ ๋ฉ์๋ ํธ์ถ : ์ ํ์ ์ธ ํํ์ด๋ค.
var data = ["book", "์ฑ
", "123"]
var Book = function(data){
this.data = data;
};
Book.prototype.concat = function(){
return this.data.concat();
};
var oneInstance = new Book(data);
log(oneInstance.concat());
'๐ฐ ์ธ์ด > JS - ๋น๊ธฐ๋ ํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ES6 - class (0) | 2021.05.25 |
---|---|
for()๊ณผ forEach() (0) | 2021.05.24 |
ํจ์ ํ๋กํผํฐ - call, apply/ bind (0) | 2021.05.24 |
์ค๋ธ์ ํธ ํ๋กํ ํ์ (0) | 2021.05.23 |
new ์ฐ์ฐ์ (0) | 2021.05.17 |