JS-5-prototype、

xiaoxiao2021-02-27  238

1.只有函数才有prototype,是函数的一个属性,指向一个对象,这个对象里面有contrutor属性。指向函数 

是共有的,大家都可以用

2.__proto__指向构造器原形属性的函数对象,与之前的函数没有关系

ie没有这个属性,只供学习用,是私有的,只有对象才有(注:函数也是对象)

构造函数创建出来的对象,有.__proto__,指向的就是prototype

总结:

对象包括普通对象(Object)和函数对象(Function),其中普通对象只有__proto__属性(原型指针),函数对象有prototype属性,prototype属性包括constructor和__proto__。

__proto__用来继承。

function定义的对象有一个prototype属性,使用new生成的对象就没有这个prototype属性。    prototype属性又指向了一个prototype对象,注意prototype属性与prototype对象是两个不同的东西,要注意区别。在prototype对象中又有一个constructor属性,这个constructor属性同样指向一个constructor对象,而这个constructor对象恰恰就是这个function函数本身

function f1(){};  var f2 = function(){};  var f3 = new Function('str','console.log(str)');  var o3 = new f1();  var o1 = {};  var o2 =new Object();  console.log(typeof Object); //function  console.log(typeof Function); //function  console.log(typeof o1); //object  console.log(typeof o2); //object  console.log(typeof o3); //object  console.log(typeof f1); //function  console.log(typeof f2); //function  console.log(typeof f3); //function 

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

tidy.__proto__=dog.prototype

dog.prototype.__proto=animal.protype

tidy.__proto__.__proto__=animal.protype

所以,方法会向上寻找,找到animal的price.即2000

转载请注明原文地址: https://www.6miu.com/read-9455.html

最新回复(0)