티스토리 뷰

728x90
SMALL

function Animal(name, animalType) {
    this.name = name;
    this.animalType = animalType;
}
Animal.prototype.sayName = function () {
    console.log(this.name);
}
Animal.prototype.sayAnimalType  = function () {
    console.log(this.animalType);
}

function Dog(name) {
    Animal.call(this, name, "Dog");
}

// copy over the methods
Dog.prototype = Object.create(Animal.prototype);
var myAnimal = new Animal("ditto", "pokemon");
myAnimal.sayName(); // "ditto"
myAnimal.sayAnimalType(); // "pokemon"
var myDog = new Dog("candy", "dog"); 
myDog.sayName(); // "candy"
myDog.sayAnimalType(); // "dog"

class Animal {
  constructor(name, animalType) {
    this.name = name;
    this.animalType = animalType;
  }
  sayName() {
    console.log(this.name);
  }
  sayAnimaltype() {
    console.log(this.animalType);
  }
}

class Dog extends Animal {}

var myanimal = new Animal("ditto", "pokemon");
myanimal.sayName();
myanimal.sayAnimaltype();

var myDog = new Dog("candy", "dog");
myDog.sayName();
728x90
LIST

'자바스크립트' 카테고리의 다른 글

자바스크립트 정리  (0) 2021.04.27
순열과 조합  (0) 2020.12.29
array.from, fill, includes  (0) 2020.12.17
객체 배열 index찾기  (0) 2020.08.30
배열 삽입, 제거  (0) 2020.08.20
댓글
공지사항