2018-01-29 원형 체인 이해

2060 단어
일반 대상과 함수 대상 개념
//  
function Person(){}
var Person = function(){}
var Person = new Function(){}

//  
var xiaohong = new Person();
// es6 , 
var limei = Object.create(Person);
var lucy={
  name:"Lucy",
  age:18
}
//  Car , : , 
function Car(brand,color){
  this.brand=brand;
  this.color=color;
  this.show=function(){
    console.log(" "+this.color+" "+this.brand);
  }
} 

//  : 
var mbh = new Car("Maybach"," ");
mbh.show();// : Maybach

//  : 
var ft = new Car("TOYOTA"," ");
ft.show();// : TOYOTA
 :
1.  
2.  , function 
3.  , new 

prototype 상속
//  
function Person(name){
  this.name = name;
}
Person.prototype.show=function(){
  console.log(" "+this.name);
}
//  
var xiaohong = new Person(" ");
xiaohong.show();// : 

var xiaoming = new Person(" ");
xiaoming.show();// : 

//  new 
var xiaohong = {};
xiaohong.__proto__= Person.prototype;
xiaohong.name = " ";
xiaohong.show();// : 
__proto__prototype 、 .해설
console.log(xiaohong.constructor);
// :function Person(name){this.name}

console.log(xiaohong.__proto__)
// :
//Object{
//  constructor:Person(name)...,
//  show:(),
//  __proto__:Object
//}
// xiaohong.__proto__ = Person.prototype. 

console.log(xiaohong.__proto__.__proto__)
//  => console.log(Person.prototype.__proto__)
// :Object
// :xiaohong -> Person  -> Object.prototype

console.log(xiaohong.__proto__==Person.prototype)// :true
console.log(xiaohong.constructor==Person.prototype.constructor)// :true
console.log(Person.prototype.constructor.prototype==Person.prototype)// :true
console.log(Person.prototype.__proto__==Object.prototype)// :true
console.log(xiaohong.__proto__.__proto__.__proto__)// :null 
//  => console.log(Object.prototype.__proto__)
 :
1.  `prototype` ,`prototype` 
2.  `__proto__` 
3.  `__proto__` 
4.  、 , 
5.  Object.prototype

좋은 웹페이지 즐겨찾기