Day +15

๋น„ํŠธยท2023๋…„ 5์›” 1์ผ
0

CodeStates

๋ชฉ๋ก ๋ณด๊ธฐ
15/54
post-thumbnail

๐Ÿ‘ฉโ€๐Ÿ’ป Koans.

1. Scope.


1-1. ํ•จ์ˆ˜ ์„ ์–ธ์‹(declaration)๊ณผ ํ•จ์ˆ˜ ํ‘œํ˜„์‹(expression)์˜ ์ฐจ์ด๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

  • 
      let funcExpressed = "to be a function";
    
      expect(typeof funcDeclared).to.equal("function"); // ์„ ์–ธ์‹ ํ•จ์ˆ˜๋Š” function
      expect(typeof funcExpressed).to.equal("string"); // let funcExpressed์œผ๋กœ ์„ ์–ธ์ด ๋จผ์ € ๋˜์–ด์„œ.
    
      function funcDeclared() {
        return "this is a function declaration";
      }
    
      funcExpressed = function () {
        return "this is a function expression";
      };
    
      const funcContainer = { func: funcExpressed }; // funcContainer๋Š” funcContainer ๊ฐ์ฒด๋‚ด์— func์„ ํ˜ธ์ถœ..?
      expect(funcContainer.func()).to.equal("this is a function expression"); // ์ดˆ๊ธฐ๊ฐ’์ธ funcExpressed์— ์žฌํ• ๋‹น.
    
      funcContainer.func = funcDeclared;
      expect(funcContainer.func()).to.equal("this is a function declaration"); // funcDeclared ๋ฆฌํ„ด๊ฐ’ ํ• ๋‹น

1-2. default parameter์— ๋Œ€ํ•ด ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

  •   function defaultParameter(num = 5) {  // ๋งค๊ฐœ๋ณ€์ˆ˜ ์•ˆ์— ๊ฐ’์ด ์—†์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ 5.
        return num;
      }
    
      expect(defaultParameter()).to.equal(5);  // ๊ธฐ๋ณธ๊ฐ’ 5 ํ• ๋‹น
      expect(defaultParameter(10)).to.equal(10);  // ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ 10์„ ์ฃผ์—ˆ์œผ๋ฏ€๋กœ 10์ด ์ถœ๋ ฅ
    
      function pushNum(num, arr = []) {  // num์„ ๋ฐฐ์—ด(arr)๋กœ ์ถœ๋ ฅ
        arr.push(num);
        return arr;
      }
    
      expect(pushNum(10)).to.deep.equal([10]);
      expect(pushNum(20)).to.deep.equal([20]);
      expect(pushNum(4, [1, 2, 3])).to.deep.equal([1, 2, 3, 4]);

1-3. lexical scope์™€ closure์— ๋Œ€ํ•ด ๋‹ค์‹œ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

  • let age = 27;
        let name = "jin";
        let height = 179;
    
        function outerFn() {
          let age = 24;
          name = "jimin";
          let height = 178;
    
          function innerFn() {
            age = 26;
            let name = "suga"; //์„ ์–ธ๋œ ๋ณ€์ˆ˜๋Š” ๊ทธ ํ•จ์ˆ˜ ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋‹ค,
            return height;
          }
    
          innerFn();
    
          expect(age).to.equal(26);
          expect(name).to.equal("jimin");
    
          return innerFn;
        }
    
        const innerFn = outerFn();
    
        expect(age).to.equal(27);
        expect(name).to.equal("jimin");
        expect(innerFn()).to.equal(178);


2. Primitive & Reference


2-1. ์ฐธ์กฐ ์ž๋ฃŒํ˜•์„ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ๊ฒฝ์šฐ, ๋ฐ์ดํ„ฐ์˜ ์ฃผ์†Œ๊ฐ€ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค

  •  const overTwenty = ["hongsik", "minchul", "hoyong"];
      let allowedToDrink = overTwenty;
    
      overTwenty.push("san");
      expect(allowedToDrink).to.deep.equal([
        "hongsik",
        "minchul",
        "hoyong",
        "san",
      ]);
      overTwenty[1] = "chanyoung";
      expect(allowedToDrink[1]).to.deep.equal("chanyoung");
    
      const ages = [22, 23, 27];
      allowedToDrink = ages;
      expect(allowedToDrink === ages).to.equal(true); //๋‘ ๋ณ€์ˆ˜๊ฐ€ ๊ฐ™์€ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ.
      expect(allowedToDrink === [22, 23, 27]).to.equal(false); //๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•˜์ง€ ์•Š๊ณ  ์ฐธ์กฐ๋งŒ ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ.
    
      const nums1 = [1, 2, 3];
      const nums2 = [1, 2, 3];
      expect(nums1 === nums2).to.equal(false);
    
      const person = {
        son: {
          // === boy
          age: 9, // 20
        },
      };
    
      const boy = person.son;
      boy.age = 20;
      expect(person.son.age).to.equal(20);
      expect(person.son === boy).to.equal(true);
      expect(person.son === { age: 9 }).to.equal(false);
      expect(person.son === { age: 20 }).to.equal(false);
    1. [1, 2, 3]; : [1, 2, 3]์ด๋ผ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ heap์— ์ €์žฅ๋˜์ง€๋งŒ ๋ณ€์ˆ˜ ํ• ๋‹น์ด ๋˜์ง€ ์•Š์•„ ์ฃผ์†Œ๋Š” ์–ด๋””์—๋„ ์ €์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค.
    1. const num1 = [1, 2, 3]; : [1, 2, 3]์ด๋ผ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ heap์— ์ €์žฅ๋˜๊ณ , ๊ทธ ์ฃผ์†Œ๊ฐ€ ๋ณ€์ˆ˜ num1์— ์ €์žฅ๋œ๋‹ค.
    1. const num2 = [1, 2, 3]; : [1, 2, 3]์ด๋ผ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ heap์— ์ €์žฅ๋˜๊ณ , ๊ทธ ์ฃผ์†Œ๊ฐ€ ๋ณ€์ˆ˜ num2์— ์ €์žฅ๋œ๋‹ค.
      -> 1), 2), 3)์—์„œ ๋งํ•˜๋Š” ์ฃผ์†Œ๋Š” ์ „๋ถ€ ๋‹ค๋ฅธ ์ฃผ์†Œ์ž…๋‹ˆ๋‹ค.


3. Array


3-1. Array์˜ ๊ธฐ๋ณธ์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

  • const emptyArr = [];
        expect(typeof emptyArr === "array").to.equal(false);
        expect(emptyArr.length).to.equal(0);
    
        const multiTypeArr = [
          0,
          1,
          "two",
          function () {
            return 3;
          },
          { value1: 4, value2: 5 },
          [6, 7],
        ];
        
        expect(multiTypeArr.length).to.equal(6);  // [0, 1, 'two', ฦ’, {โ€ฆ}, Array(2)]
        expect(multiTypeArr[0]).to.equal(0); 
        expect(multiTypeArr[2]).to.equal("two");
        expect(multiTypeArr[3]()).to.equal(3);
        expect(multiTypeArr[4].value1).to.equal(4);
        expect(multiTypeArr[4]["value2"]).to.equal(5); 
        expect(multiTypeArr[5][1]).to.equal(7);


4. Object


4-1. Object์˜ ๊ธฐ๋ณธ์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

  • const emptyObj = {};
        expect(typeof emptyObj === "object").to.equal(true);
        expect(emptyObj.length).to.equal(undefined); // Object.keys()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ ํ›„ .length๋กœ ๊ธธ์ด ํ™•์ธ
    
        const megalomaniac = {
          mastermind: "Joker",
          henchwoman: "Harley",
          getMembers: function () {
            return [this.mastermind, this.henchwoman];
          },
          relations: ["Anarky", "Duela Dent", "Lucy"],
          twins: {
            "Jared Leto": "Suicide Squad",
            "Joaquin Phoenix": "Joker",
            "Heath Ledger": "The Dark Knight",
            "Jack Nicholson": "Tim Burton Batman",
          },
        };
    
        expect(megalomaniac.length).to.equal(undefined);
        expect(megalomaniac.mastermind).to.equal("Joker");
        expect(megalomaniac.henchwoman).to.equal("Harley");
        expect(megalomaniac.henchWoman).to.equal(undefined);
        expect(megalomaniac.getMembers()).to.deep.equal(["Joker", "Harley"]);
        expect(megalomaniac.relations[2]).to.equal("Lucy");
        expect(megalomaniac.twins["Heath Ledger"]).to.deep.equal("The Dark Knight");


5. Destructuring


5-1. rest/spread ๋ฌธ๋ฒ•์„ ๊ฐ์ฒด ๋ถ„ํ•ด์— ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค #3

  • const user = {
          name: "๊น€์ฝ”๋”ฉ",
          company: {
            name: "Code States",
            department: "Development",
            role: {
              name: "Software Engineer",
            },
          },
          age: 35,
        };
    
        const changedUser = {
          ...user,
          name: "๋ฐ•ํ•ด์ปค", 
          age: 20,
        };
    
        const overwriteChanges = {
          name: "๋ฐ•ํ•ด์ปค",
          age: 20,
          ...user,  //spread ๋ฌธ์ด ๋’ค์— ์˜ค๋ฉด ์•ž์— ํ• ๋‹น๋œ ๋ฌธ๋ฒ•์€ ์ดˆ๊ธฐํ™”
        };
    
        const changedDepartment = {
          ...user,
          company: {
            ...user.company,
            department: "Marketing",
          },
        };
    
        expect(changedUser).to.eql({
          name: "๋ฐ•ํ•ด์ปค",
          company: {
            name: "Code States",
            department: "Development",
            role: {
              name: "Software Engineer",
            },
          },
          age: 20,
        });
    
        expect(overwriteChanges).to.eql({
          name: "๊น€์ฝ”๋”ฉ",
          company: {
            name: "Code States",
            department: "Development",
            role: {
              name: "Software Engineer",
            },
          },
          age: 35,
        });
    
        expect(changedDepartment).to.eql({
          name: "๊น€์ฝ”๋”ฉ",
          company: {
            name: "Code States",
            department: "Development",
            role: {
              name: "Software Engineer",
            },
          },
          company: {
            name: "Code States",
            department: "Marketing",
            role: {
              name: "Software Engineer",
            },
          },
          age: 35,
        });


โšก์ฐธ์กฐ


profile
Drop the Bit!

0๊ฐœ์˜ ๋Œ“๊ธ€