자 바스 크 립 트 원형 데이터 공유 와 방법 공유 실현 탐구
                                            
 4118 단어  JavaScript데이터 공유방법 공유
                    
어떤 데 이 터 를 원형 에 써 야 합 니까?
공유 할 데이터 가 필요 하면 원형 을 쓸 수 있 습 니 다.
원형 역할 중 하나:데이터 공유
속성 은 공유 해 야 하고 방법 도 공유 해 야 합 니 다:
다음은 사례 를 살 펴 보 겠 습 니 다.
데이터 공유 사례
모든 학생 의 이름,나이,성별 은 독특 하 므 로 우 리 는 설정 해 야 한다.
모든 학생 의 키 는 188 이 고 모든 사람의 체중 은 55 이다.
모든 학생 들 은 매일 500 줄 의 코드 를 써 야 한다.
모든 학생 들 은 매일 10 근 의 수박 을 먹 어야 한다.
공유 데 이 터 를 원형 에 쓸 수 있 습 니 다.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>title</title>
  <script>
    function Student(name,age,sex) {
      this.name=name;
      this.age=age;
      this.sex=sex;
    }
    //          188,        55
    //          500   
    //            10    
    //    
    Student.prototype.height="188";
    Student.prototype.weight="55kg";
    Student.prototype.study=function () {
      console.log("  , 500   ,    ");
    };
    Student.prototype.eat=function () {
      console.log("   10    ");
    };
    //     ,    
    var stu=new Student("  ",57," ");
    console.dir(Student);
    console.dir(stu);
//    stu.eat();
//    stu.study();
  </script>
</head>
<body>
</body>
</html>
 
 원형 단순 표기 법
원형 은 더 간단 한 방법 이 있 는데 다음은 상기 사례 에 대한 수정 이다.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>title</title>
  <script>
    function Student(name, age, sex) {
      this.name = name;
      this.age = age;
      this.sex = sex;
    }
    //       
    Student.prototype = {
      //          
      constructor:Student,
      height: "188",
      weight: "55kg",
      study: function () {
        console.log("      ");
      },
      eat: function () {
        console.log("      ");
      }
    };
    var stu=new Student("    ",20," ");
    stu.eat();
    stu.study();
    console.dir(Student);
    console.dir(stu);
  </script>
</head>
<body>
</body>
</html>
 
 원형 방법 공유
예 를 들 어 방법 을 설정 하고 먹고 놀 면 놀 고 끝나 면 잔다.
 
 
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>title</title>
  <script>
    //      ,        
    function Animal(name,age) {
      this.name=name;
      this.age=age;
    }
    //       
    //      
    Animal.prototype.eat=function () {
      console.log("     ");
      this.play();
    };
    //      
    Animal.prototype.play=function () {
      console.log("  ");
      this.sleep();
    };
    Animal.prototype.sleep=function () {
      console.log("   ");
    };
    var dog=new Animal("  ",20);
    dog.eat();
    //        ,      
  </script>
</head>
<body>
</body>
</html>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기초 정리 - 1문자 (String) 숫자 (Number) 불린 (Boolean) null undefined 심볼 (Symbol) 큰정수 (BigInt) 따옴표로 묶어 있어야 함 Not-A-Number - 숫자 데이터 / 숫자로 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.