區別一下,普通對象和函數對象。函數對象纔有prototype屬性。函數
函數對象:prototype
function f1(){}; var f2 = function(){}; var f3 = new Function('str','console.log(str)');
普通對象:code
var o3 = new f1(); var o1 = {}; var o2 =new Object();
簡單的說,凡是經過 new Function() 建立的對象都是函數對象,其餘的都是普通對象。
原型的做用就是繼承。對象
原型鏈:
JS在建立對象(不管是普通對象仍是函數對象)的時候,都有一個叫作__proto__的內置屬性,用於指向建立它的函數對象的原型對象prototype。
原型對象prototype中都有個預約義的constructor屬性,用來引用它的函數對象。繼承
person.prototype. constructor === person //true Function.prototype.constructor === Function //true Object.prototype.constructor === Object //true
總結一下:ip
var animal = function(){}; var dog = function(){}; animal.price = 2000;// dog.prototype = animal; var tidy = new dog(); console.log(dog. price) //undefined console.log(tidy.price) // 2000
事實證實,真正起做用的不是prototype ,而是__proto__。原型鏈