gpt4 book ai didi

javascript - 函数的__proto__

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:07:36 25 4
gpt4 key购买 nike

如果我有一个名为 Person 的类。

var Person =  function(fname, lname){
this.fname = fname;
this.lname = lname;
}

Person.prototype.mname = "Test";
var p = new Person('Alice','Bob');

现在,p.__proto__ 指的是 Person 的原型(prototype),但是当我尝试执行 Person.__proto__ 时,它指向 function(),并且 Person.constructor 指向 Function()

谁能解释一下 function()Function() 之间的区别以及为什么 Function() 类的原型(prototype)是一个 function()?

最佳答案

Can someone explain what is the difference between function() and Function() and why the prototype of a Function() class is a function()?

__proto__ 是公开 [[prototype]] 的实现细节。 [[prototype]] 和构造函数不需要(通常不是)相同的东西。无论如何...

考虑这个假设:这是一个暗示。取决于引擎的细节——在测试的特定引擎中(FF,哪个版本?),Function 是一个对象,它本身有一个 [[prototype]] 函数。 function 是原始函数对象。 Person.prototype(默认情况下)是函数类型(原始函数对象),断言是这种明显二分法的结果。 (JS 有一些怪癖:new Number(0)0 不同。)

但是,在 IE(8) 中情况并非如此。在 IE 中,默认原型(prototype)是“普通对象”,而不是函数对象。

关于javascript - 函数的__proto__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2872257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com