__proto__ of a function
If I have a class called Person.
var Person = function(fname, lname){
this.fname = fname;
this.lname = lname;
}
Person.prototype.mname = "Test";
var p = new Person('A开发者_运维问答lice','Bob');
Now, p.__proto__
refers to prototype of Person but, when I try to do Person.__proto__
, it points to function()
, and Person.constructor
points to Function()
.
Can someone explain what is the difference between function()
and Function()
and why the prototype of a Function()
class is a function()
?
Can someone explain what is the difference between function() and Function() and why the prototype of a Function() class is a function()?
__proto__
is an implementation-detail exposing the [[prototype]]. The [[prototype]] and the constructor need not be (are often not) the same thing. Anyway...
Consider this hypothesis: It is an impl. detail that depends on the engine -- and in the particular engine tested (FF, which version?), Function is an object which itself has a [[prototype]] of function. function is the primitive function-object. Person.prototype is (by default) of type function (the primitive function-object) and the assertions stated as a result of this apparent dichotomy. (JS has some quirks: new Number(0)
is not the same as 0
.)
However, this is not the case in IE(8). In IE the default prototype is a "plain object", not a function-object.
When defining a function like this:
var Person = function (fname, lname){
this.fname = fname;
this.lname = lname;
}
This will make Person
a function. A function is an object, which needs to be constructed like any other 'first class' object, so it has a constructor: the constructor of all function objects, called Function
.
The prototype of all functions appears to be an object called function
.
I like to refer to a nice explanation by Mike Koss that learned me a lot.
精彩评论