๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ฐ ์–ธ์–ด/JS - ๋น„๊ธฐ๋„ˆ ํŽธ

๋ฉ”์†Œ๋“œ

MDN_Method > 

 

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());