Javascript 상속

개발 노트 2009. 5. 8. 16:52 posted by 무병장수권력자



작성자 : 김문규
최초 작성일 : 2009.5.8

Animal 객체를 Dog 객체가 상속하고 이를 사용하는 모습에 대한 예제입니다.

<script type='text/javascript'>       

Animal = function() {
    this.name = "nobody"
    this.speak = function () {
        return "Who am I?"
    }
}
Dog = function() {
  this.speak = function() {
    return "Woof!"
  }
}
Dog.prototype = new Animal();

myAnimal = new Dog();
alert('The animal named ' + myAnimal.name + ' says ' + myAnimal.speak());

</script>