简体   繁体   English

call()和apply()Javascript?

[英]call() and apply() Javascript?

var a = function() { }; //Just a function

When I run a.__proto__ in the console, it returned this 当我在控制台中运行a.__proto__时,它返回了

function () {}

So, if prototype of every function created in javascript is a function like the above, then where exactly methods like call(), apply() and bind() defined? 因此,如果用javascript创建的每个函数的原型都是上述函数,那么在哪里定义了像call(),apply()和bind()这样的方法呢?

The functions do exist on a.__proto__ (or further in the chain) - the issue you're seeing is more of how it's being represented in the console. 这些函数确实存在于a.__proto__ (或在链中的更a.__proto__ )上-您看到的问题更多是它在控制台中的表示方式。 For instance, if you just do a in the console, you'd also see the empty function string, but without the __proto__ property, which you already know is there. 举例来说,如果你只是做a控制台,你也看到了空函数字符串,但没有__proto__属性,你已经知道是存在的。

As @squint pointed out in the comments , if you use console.dir the other properties will be shown: 正如@squint 在注释中指出的那样,如果使用console.dir ,则将显示其他属性:

在此处输入图片说明

if prototype of every function created in javascript is a function like the above, then where exactly methods like call(), apply() and bind() defined? 如果用javascript创建的每个函数的原型都是上述函数,那么在哪里定义了像call(),apply()和bind()这样的方法呢?

Exactly on that function / pobject. 正是在那个功能/ pobject上。 Function.prototype is a function, but it is not like any other. Function.prototype是一个函数,但与其他函数不同。 It's defined §8.2.2 CreateIntrinsics : 它是在§8.2.2CreateIntrinsics中定义的:

8. Let noSteps be an empty sequence of algorithm steps. 8.令noSteps为空的算法步骤序列。
9. Let funcProto be CreateBuiltinFunction ( realmRec , noSteps , objProto ). 9.让funcProtoCreateBuiltinFunctionrealmRecnoStepsobjProto )。
10. Set intrinsics .[[%FunctionPrototype%]] to funcProto . 10.将内部函数 。[[%FunctionPrototype%]]设置为funcProto

It is a built-in function, but its own prototype is set to the default object prototype ( objProto ) here, not to itself (you cannot have circular prototype chains). 它是一个内置函数,但其​​自身的原型在此处设置为默认的对象原型( objProto ),而不是其自身(您不能具有圆形原型链)。

Step 13 in that algorithm then goes and says that all intrinsic values need to be recursively initialized according to some people and certain sections in the spec: 然后,该算法中的第13步继续进行,并说需要根据规范中的某些人员和某些部分以递归方式初始化所有内在值:

Set fields of intrinsics with the values listed in Table 7 that have not already been handled above. 使用表7中列出的尚未设置的内部值设置内部字段。 The field names are the names listed in column one of the table. 字段名称是表第一列中列出的名称。 The value of each field is a new object value fully and recursively populated with property values as defined by the specification of each object in clauses 18-26. 每个字段的值是一个新的对象值,该对象值完全并递归地填充有第18-26节中每个对象的规范所定义的属性值。 [...] [...]

The table contains: 该表包含:

%FunctionPrototype% | %FunctionPrototype% | Function.prototype | Function.prototype原型 The initial value of the prototype data property of %Function% %Function%的原型数据property的初始值

and §19.2.3 describes all the properties of that object, including the ones you mentioned. 和第19.2.3节描述了该对象的所有属性,包括您提到的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM