简体   繁体   English

Javascript中的“功能对象”一词实际上是什么意思?

[英]What does the term `functions objects` in Javascript actually mean?

After reading a lot, I still don't quite understand the term function objects in JS. 看了很多书后,我仍然不太了解JS中的function objects一词。 From what I have read, following are my interpretations. 根据我的阅读,以下是我的解释。 Please correct if I'm wrong. 如果我错了,请更正。

function myfunc(x){
 return x;
}

Interpretation : myfunc is a function object auto created by JS interpreter internally whenever a new function like myfunc is declared as shown above. 解释myfunc是由JS解释器在内部自动创建的function object ,只要声明了新函数(如myfunc ),如上所示。

var myObj = new myfunc();
var mynewObj = new myfunc();

Interpretation : myObj and mynewObj are instances(objects) of myfunc thus can be said to be a function objects 解释myObjmynewObjmyfunc实例(对象),因此可以说是一个function objects

Are my interpretations correct. 我的解释正确吗? Any other examples of function objects? 函数对象还有其他示例吗?

thanks 谢谢

bt bt

Interpretation: myfunc is a function object auto created by JS interpreter internally whenever a new function like myfunc is declared as shown above. 解释: myfunc是由JS解释器在内部自动创建的函数对象,只要声明了新函数(如myfunc ),如上所示。

Almost, but not quite. 差不多,但是不完全是。 myfunc is an identifer that refers to (points to) a function created by the JavaScript engine. myfunc是一个标识符,它引用 (指向)由JavaScript引擎创建的函数。 1 That function is an object, because all functions are objects in JavaScript. 1该函数是一个对象,因为所有函数都是JavaScript中的对象。 You could call it a "function object" if you like; 如果愿意,可以将其称为“功能对象”。 most people would just call it a "function." 大多数人会称其为“功能”。 (In JavaScript "function" and "function object" are synonyms.) (在JavaScript中,“函数”和“函数对象”是同义词。)

 var myObj = new myfunc(); var mynewObj = new myfunc(); 

Interpretation: myObj and mynewObj are instances(objects) of myfunc thus can be said to be a function objects 解释: myObjmynewObjmyfunc实例(对象),因此可以说是一个function objects

No, they're not function objects. 不,它们不是功能对象。 They're just objects. 它们只是对象。 They are indeed instanceof myfunc which means that the object myfunc.prototype points to is in their prototype chain, but it's not correct at all to call them "function objects" because they aren't functions. 它们确实是instanceof myfunc ,这意味着myfunc.prototype指向的对象位于其原型链中,但是将其称为“函数对象”根本不是正确的,因为它们不是函数。


1 "JavaScript engine" - This is the term I use in preference to "JavaScript interpreter" because any modern JavaScript engine is a just-in-time-compiler plus runtime environment, not an interpreter. 1个 “ JavaScript引擎”-这是我优先使用的术语,而不是“ JavaScript解释器”,因为任何现代JavaScript引擎都是即时编译器加上运行时环境,而不是解释器。 It's a subtle distinction. 这是一个微妙的区别。


In a comment, Jamie Dixon mentions the Function function. Jamie Dixon在评论中提到了Function函数。 It's just a function that creates functions based on source code strings. 它只是一个基于源代码字符串创建函数的函数。 There are virtually no use cases for it in modern JavaScript (just as there are virtually no use cases for eval in modern JavaScript). 有几乎没有用例为它在现代的JavaScript(就像有几乎没有用例eval现代的JavaScript)。

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

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