constructor, prototype, object.create()

column clash·2021년 8월 18일
0
 function 생성함수(과일이름) {
        this.name = 과일이름;
        this.price = 1500;
        this.alim = function () {
          console.log("세일 중입니다." + this.name + this.price + "입니다.");
        };
      } // 이게 constructor,생성자   여기서 this는 새로 생성되는 object (인스턴스)
      
       var 과일1 = new 기계("바나나가");
      var 과일2 = new 기계("토마토가");
      과일1.alim();
      
      new 어쩌고가 상속 
      ( 생성함수가 가지고 있는 name, price, alim 속성들을 물려받음)
      
      
        // 상속을 구현할 수 있는 또 하나의 문법 prototype

      //생성함수(constructor) 를 만들면 prototype 이라는 공간이 자동으로 생긴다. (prototype은 유전자)
      
      
      // 사람이 키가 크거나 작거나 부모님에게 유전자를 물려받아서 그런다. 
      prototype 에 값을 추가하면 모든 자식들이 물려받기 가능
      
      
      생성함수.prototype.location = "하나마트";
      // 활용만 할거면 여기까지만 알면 됨
      
      //prototype의 동작원리
      
      
      //prototype의 동작원리
        var 과일1 = new 기계("바나나가");
      var 과일2 = new 기계("토마토가");
      과일1.alim();
      

      // 과일1.location
      // (1) 과일1 직접 location 를 가지고 있나? 없넹
      // (2) 그럼 과일1의 부모 유전자가 location 를 가지고 있나? 
      생성함수.prototype 아 있네!
      // 오브젝트는 이런 순서로 자료를 출력한다.
      // 내가 location 가 없으면 부모 유전자에서 찾는구나~
      
      // 그렇다면 과일1.toString() 은?  [1,2,3].sort(); 내장함수를 쓸 수 있는이유>
      // 과일1에는 toString 이 없다.
      // 내가 없으면 부모의 prototype 을 찾아본다.
      // 그럼 부모의 부모 유전자에 있는가? x 위로 위로.
      // 부모의 부모 유전자에 있기때문에 쓸 수 있다.
      
      // 과일1.toString (toString이 없네) => 
      생성함수.prototype(toString이 없네) => Object.prototype (있네)

prototype 특징

//prototype 특징.
      //1 . 함수에만 생김.
      var arr = [1, 2, 3];
      var arr2 = new Array(1, 2, 3);
      var obj = { name: "kim" };
      var obj2 = new Object();

      // arr.prototype  undefined
      // arr2.prototype undefined

      //2. 유전작검사 _proto_  부모 유전자를 출력해주세요.
      console.log("학생1은", 학생1.__proto__);

      var 부모 = { name: "kim" };
      var 자식 = {};
      자식.__proto__ = 부모; //이런식으로 편법을 쓰면 나의 부모유전자는 이걸로 해주세요.
      console.log(자식.name); // 을 쓸 수 있다.
 function Parent() {
        this.name = "Kim";
      }
      var a = new Parent();

      a.__proto__.age = 30;

      var b = new Parent();
      console.log(b.age);

      function UserCreator2(name, age) {
        this.name = name;
        this.age = age;
      }

      UserCreator2.prototype.sayHi = function () {
        console.log("안녕 나는" + this.name + "이야");
      };

      var newUser = new UserCreator2("test", 11);
      newUser.sayHi();
Array.prototype.remove3 = function () {
        for (var i = 0; i < this.length; i++) {
          if (this[i] === 3) {
            this.splice(i, 1);
            i--;
          }
        }
      };

      var arr = [1, 2, 3];
      arr.remove3();
      console.log(arr);
 var 부모 = { name: "Kim", age: 50 };
      var 자식 = Object.create(부모);
      //프로퍼티를 정의한적이 없다. 직접 name, age 를 지정한게 아니라. prototype 을 부모로해주세요~
      console.log(자식); // {}
      console.log(자식.name); //"kim"

      // 자식이 name 을 직접 가지고 있나?  그럼 자식의 부모 prototype 에는 name 이 있나?
      자식.age = 20; //자식.age 20

      var 손자 = Object.create(자식);
      console.log(손자); //{}
      console.log(손자.name); //"kim"
      console.log(손자.age); //"20"
profile
풀스택 개발 중...

0개의 댓글