JS 깊이 이해 (3) 대상 지향
5659 단어 JS
봉인
패키지: 디 테 일 숨 기기
프로 토 타 입 체인: 대상 과 대상 this: 대상 은 함수 (JS 에서 대상 함수 와 관계 가 없습니다. JS 의 아버 지 는 this 를 통 해 강제로 관계 가 있 습 니 다.) 함 수 는 대상 의 부속품 이 아 닙 니 다.
이것 에 대하 여:
fn(this,arguments)
__proto__
봉 인 된 사상 약정 이니셜 대문자 로 사용자 정의 return 을 무시 하지 마 십시오.JS 로 자바 대상 자바 클래스 구현:
//
function Human(options){
this.name = options.name
this. = options.
}
Human.prototype.eat = function(){}
Human.prototype.drink = function(){}
Human.prototype.poo = function(){}
function Soldier(options){
// this.__proto__ = Soldier.prototype
Human.call(this, options)
this.ID = options.ID
this. = 42
}
// createSoldier.prototype = {constructor: createSoldier}
// ie
// function fakeHuman(){}
// fakeHuman.prototype = Human.prototype
// Soldier.prototype = new fakeHuman()
// no-ie
Soldier.prototype = Object.create(Human.prototype)
//
// Soldier.prototype.__proto__ === Human.prototype
Soldier.prototype. = " "
Soldier.prototype. = 5
Soldier.prototype. = function(){ /* */},
Soldier.prototype. = function(){ /* */ },
Soldier.prototype. = function(){ /*Go die*/ },
Soldier.prototype. = function(){ /* */ },
Soldier.prototype. = function(){ /* */ }
// Soldier.prototype.__proto__ = Human.prototype
var s = new Soldier({name: ' ', :'yellow', ID: 1})
// 1. __proto__
// Soldier.prototype.__proto__ = Human.prototype
// Soldier.prototype.__proto__ === this.__proto__ === Human.prototype
// class
class Human{
constructor(options){
this.name = options.name
this. = options.
}
eat(){}
drink(){}
poon(){}
}
class Soldier extends Human{
constructor(options){
super(options)
this.ID = options.ID
this. = 42
this. = " "
this. = 5
}
(){ /* */}
(){ /* */ }
(){ /*Go die*/ }
(){ /* */ }
(){ /* */ }
}
var s = new Soldier({name: ' ', :'yellow', ID: 1})