prototype
- 原型:本身是个对象,在javascript中每个实例化对象,都有一个原型 __proto__,指向该构造器的原型对象。
- 构造器的原型对象:所有该构造器实例化的对象,都可共享其原型对象的属性和方法。
- 原型链:当调用实例化对象的属性和方法时,对象首先会查找本身的属性和方法。若没有,则在其原型上查找,并会逐级查找父原型,直到最后一级null止,从而形成原型链。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>
console.log(Object.prototype);  //[object Object] { ... }
console.log(Object.__proto__);  //function() {[native code]}
console.log(Object.prototype.prototype===undefined);  //true
console.log(Object.prototype.__proto__===null);  //true
console.log(Object.__proto__===Object.prototype);  // false
console.log(Object.__proto__===Function.prototype);  // true
console.log(Object.prototype===Function.prototype);  // false
console.log("-----------------------------")
var Person=function(){};
  
console.log(Person.__proto__===Function.prototype);  // true
console.log(Person.__proto__===Object.__proto__);  // true
console.log(Person.__proto__===Person.prototype);  // false
console.log("-----------------------------")
Person.prototype={
  say:function(){
    console.log("hello111");
  }
};
var p=new Person();
console.log(p.__proto__===Person.prototype);  // true
Person.prototype={
   say:function(){
        console.log("hello222");
   }
}
var t=new Person();
console.log(t.__proto__===Person.prototype);  // true
t.say();  // hello111
console.log(p.__proto__===Person.prototype);  // false
p.say();  // hello222
  
  
  var foo = {},
    F = function(){};
Object.prototype.a = 'value a';
Function.prototype.b = 'value b';
console.log(foo.a)    // value a
console.log(foo.b)    // undefined
console.log(F.a)      // value a
console.log(F.b)      // value b
</script>
</body>
</html>
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号