正如你自己所说:你有一个函数object。函数是 JS 中的对象,就像对象字面量、数组或其他任何东西一样:函数可以随意分配属性和方法:
var someAnonFunction = function(foo)
{
console.log(this);
console.log(this === someAnonFunction);//will be false most of the time
};
someAnonFunction.x = 123;//assign property
someAnonFunction.y = 312;
someAnonFunction.divide = function()
{
console.log(this === someAnonFunction);//will be true most of the time
return this.x/this.y;//divide properties x & y
};
someAnonFunction.divide();
在这种情况下,由someAnonFunction 引用的函数对象被分配了对匿名函数的引用,称为divide(好吧,对匿名函数的引用无论如何都被称为除法)。所以这里根本没有原型参与。请注意,正如您自己所说:所有对象都可以追溯到Object.prototype,试试这个:
console.log(someAnonFunction.toString === Function.prototype.toString);//functions are stringified differently than object literals
console.log(someAnonFunction.hasOwnProperty === Object.prototype.hasOwnProperty);//true
或者,也许这更清楚:如何将方法/属性调用解析为 JS 中的值的简单方案:
[ F.divide ]<=========================================================\ \
F[divide] ===> JS checks instance for property divide | |
/\ || | |
|| || --> property found @instance, return value-------------------------------| |
|| || | |
|| ===========> Function.prototype.divide could not be found, check prototype | |
|| || | |
|| ||--> property found @Function.prototype, return-----------------------| |
|| || | |
|| ==========> Object.prototype.divide: not found check prototype? | |
|| || | |
|| ||--> property found @Object.prototype, return---------------------|_|
|| || |=|
|| =======>prototype is null, return "undefined.divide"~~~~~~~~~~~~~~~|X|
|| \ /
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~< TypeError can't read property 'x' of undefined
因此,如果您希望上面的代码使用原型工作,您将不得不增加各种原型(在本例中为Function.prototype)。请注意,不建议这样做,实际上更改 "native" 原型通常是不受欢迎的。还是:
Function.prototype.divide = function (a, b)
{
a = +(a || 0);//coerce to number, use default value
b = +(b || 1) || 1;//division by zeroe is not allowed, default to 1
return a/b;
};
function someFunction ()
{
return 'someString';
};
var another = function(a, b)
{
return a + b;
};
someFunction.divide(12, 6);//will return 2
another.divide(12, 4);//3
在这两种情况下,名称(someFunction 或 another)引用的函数对象将被扫描以查找名为 divide 的属性,但未找到该属性。然后它会扫描Function.prototype,找到这样的属性。
如果不是这样,JS 也会检查Object.prototype,如果失败,最终会抛出错误。
不久前我就这个主题发布了相当长的答案:
What makes my.class.js so fast?(处理原型链)
Objects and functions in javascript(函数对象构造函数的回顾)
What are the differences between these three patterns of "class" definitions in JavaScript?(还有更多信息)
Javascript - Dynamically change the contents of a function (隐约涉及匿名函数,分配给变量和属性并更改它们的上下文)