原型、原型鏈

每次面試都要從新鞏固下這個知識點,決定仍是本身概括下面試

首先貼個《Javascript高級程序設計》的圖函數

 

1、知識點:this

1.普通對象與函數對象spa

var o1 = {}; 
var o2 =new Object();
var o3 = new f1();

function f1(){}; 
var f2 = function(){};
var f3 = new Function('str','console.log(str)');

console.log(typeof Object); //function 
console.log(typeof Function); //function  

console.log(typeof f1); //function 
console.log(typeof f2); //function 
console.log(typeof f3); //function   

console.log(typeof o1); //object 
console.log(typeof o2); //object 
console.log(typeof o3); //object
 o1 o2 o3 爲普通對象,
f1 f2 f3 爲函數對象。
凡是經過 new Function() 建立的對象都是函數對象,其餘的都是普通對象。f1,f2,歸根結底都是經過 new Function()的方式進行建立的。Function Object 也都是經過 New Function()建立的

2.實例的構造函數屬性(constructor)指向構造函數。prototype

console.log(person1.constructor == Person); //true

3.每一個對象都有 __proto__ 屬性,但只有函數對象纔有 prototype 屬性設計

4.原型對象就是 Person.prototype 指針

Person.prototype = {
   name:  'Zaxlct',
   age: 28,
   job: 'Software Engineer',
   sayName: function() {
     alert(this.name);
   }
}

在默認狀況下,全部的原型對象都會自動得到一個 constructor(構造函數)屬性,這個屬性(是一個指針)指向 prototype 屬性所在的函數(Person)code

5.這個鏈接存在於實例(person1)與構造函數(Person)的原型對象(Person.prototype)之間,而不是存在於實例(person1)與構造函數(Person)之間。對象

person1.constructor == Person
Person.prototype.constructor == Person
person1.__proto__ == Person.prototype

2、劃重點:blog

  1. person1.__proto__ 是什麼?
  2. Person.__proto__ 是什麼?
  3. Person.prototype.__proto__ 是什麼?
  4. Object.__proto__ 是什麼?
  5. Object.prototype__proto__ 是什麼?

答案:
第一題:
由於 person1.__proto__ === person1 的構造函數.prototype
由於 person1的構造函數 === Person
因此 person1.__proto__ === Person.prototype

第二題:
由於 Person.__proto__ === Person的構造函數.prototype
由於 Person的構造函數 === Function
因此 Person.__proto__ === Function.prototype

第三題:
Person.prototype 是一個普通對象,咱們無需關注它有哪些屬性,只要記住它是一個普通對象。
由於一個普通對象的構造函數 === Object
因此 Person.prototype.__proto__ === Object.prototype

第四題,參照第二題,由於 Person 和 Object 同樣都是構造函數

第五題:
Object.prototype 對象也有proto屬性,但它比較特殊,爲 null 。由於 null 處於原型鏈的頂端,這個只能記住。
Object.prototype.__proto__ === null

相關文章
相關標籤/搜索