It is said that all Javascript objects have a prototype property, but I only see foo.prototype if foo is a function?

前端 未结 2 879
深忆病人
深忆病人 2020-12-28 17:21

It is often said that every Javascript object has a prototype property, but I find that foo.prototype has a value only if foo is a fun

2条回答
  •  渐次进展
    2020-12-28 18:22

    It is the constructor of every Object that has a prototype. So for some foo, baror foobar:

    var foo = {};
    console.log(foo.constructor.prototype); //=> Object
    var bar = 5;
    console.log(bar.constructor.prototype); //=> Number
    function Foobar(){} 
    var foobar = new Foobar; //Foobar used a constructor
    console.log(foobar.constructor.prototype); //=> Foobar
    

提交回复
热议问题