javascript function 객체(상속)
함수 객체가 만들어질때, 함수를 생성하는 Function 생성자는 다음과 같은 코드를 실행합니다
this.prototype = { constructior : this }
새로운 함수객체는, 새로운 함수객체를 값으로 갖는 constructor 라는 속성이 있는 객체를 prototype 속성에 할당 받습니다.
모든 함수는 prototype 속성을 갖습니다. prototype 객체 내에 기본적으로 할당되는 constructor 속성은 유용하지 않습니다. 중요한 것은 prototype 객체 자체이다
var Mammal = function (xname ) { this.xname = xname; }
Function.prototype.isPrototypeOf( Mammal ) //===> true
Object. prototype.isPrototypeOf( Function ) //===> true
Object. prototype.isPrototypeOf( Mammal ) //===> true
Mammal.prototype.constructor // ===> f (xname ) { this.xname = xname; }
Mammal.prototype.constructor.name // ===> "Mammal"
Mammal.prototype.get_xname = function () { return this.xname; }
Mammal.prototype.says = function () { return this.saying || ''; }
var myMammal = new Mammal('Herb the Mammal');
var name = myMammal.get_xname(); //===> "Herb the Mammal"
var says = myMammal.says(); //===> ""
Mammal. prototype.isPrototypeOf( myMammal ) //===> true
Function.prototype.isPrototypeOf( myMammal ) //===> false
Object. prototype.isPrototypeOf( myMammal ) //===> true
myMammal.xname //===> "Herb the Mammal"
myMammal.saying //===> undefined
myMammal.prototype //===> undefined
myMammal.__proto__ //===> {get_xname: ƒ, says: ƒ, constructor: ƒ}
myMammal.myMammalHello = function() { return "myMammal Hello"};
myMammal.myMammalHello(); //===> "myMammal Hello"
Mammal.myMammalHello //===> undefined
Mammal.myMammalHello() //===> Uncaught TypeError: Mammal.myMammalHello is not a function
var Cat = function (name) { this.name=name; this.saying = 'meow'; }
Function.prototype.isPrototypeOf( Cat ) //===> true
Object. prototype.isPrototypeOf( Function ) //===> true
Object. prototype.isPrototypeOf( Cat ) //===> true
Cat.prototype.constructor // ===> ƒ (name) { this.name=name; this.saying = 'meow'; }
Cat.prototype.constructor.name // ===> "Cat"
// Cat.prototype 을 Mammal 의 새 인스턴스로 대체.
Cat.prototype = new Mammal( );
Cat.prototype // ===> Mammal {xname: undefined}
Cat.prototype.constructor // ===> ƒ (xname ) { this.xname = xname; }
Cat.prototype.constructor.name // ===> "Mammal"