admin管理员组文章数量:1279145
So as long as anything in Javascript is actually an object, what makes an object behave as a function? What internal properties and labels make an object behave as an object we can call instead of just using it to store values?
So as long as anything in Javascript is actually an object, what makes an object behave as a function? What internal properties and labels make an object behave as an object we can call instead of just using it to store values?
Share Improve this question edited Feb 3, 2020 at 14:10 Isaaс Weisberg 2,8243 gold badges17 silver badges31 bronze badges asked Feb 3, 2020 at 13:39 SusanoSusano 2844 silver badges10 bronze badges 4- Can you provide sample code – Tiisetso Tjabane Commented Feb 3, 2020 at 13:41
- ecma-international/publications/standards/Ecma-262.htm – Heretic Monkey Commented Feb 3, 2020 at 13:42
- Only native functions and things created from the Function constructor/class are functions. You cannot convert a non-function object into a function – slebetman Commented Feb 3, 2020 at 13:43
- 4 If an object implements the internal [[Call]] method, then it will be callable: tc39.es/ecma262/#table-6 – Nick Parsons Commented Feb 3, 2020 at 13:51
6 Answers
Reset to default 6There is an internal property, [[Call]]
, which determines what will be executed when the object is called.
Plain objects don't have this internal property, so they are not callable, and can not be made callable.
The only callables in JS are:
- functions (which are also objects),
- classes (which are actually functions, and therefore objects as well), and
- Proxy objects that wrap callables.
To create a callable object, you must create a function and make it have the properties and prototypes you want.
So as long as anything in javascript is actually an object
Well, not everything in JavaScript is an object. For instance, primitives such as strings, booleans and numbers are not objects. JavaScript just wraps these primitives in objects when properties are accessed.
what makes an object behave as a function what internal properties and labels did this job to make an object behave as an execution code instead of just storing variables
You're correct about functions being objects, however, unlike regular objects which just store key-value pairs, function objects have a special internal method which they implement known as [[Call]]
. This call method is what executes the code associated with the function object and is invoked when you call a function (func_name()
). So, any object which implements the internal [[Call]]
method is callable. You yourself cannot make an object callable by implementing this property as it is not part of the ECMAScript language itself, but rather a method name used by the spec. Instead, we use the function
keyword for that. Function objects are still objects at the end of the day, so you can assign properties to them as well as access preexisting properties:
function foo(a) {
return a;
}
foo.bar = "foobar";
console.log(foo.length); // able to access pre-existing properties
console.log(foo.bar); // able to access our own properties
console.log(foo(2)); // able to invoke (due to [[Call]])
You can read more about function objects here
You could assign an object to a function to get a callable function.
var object = { foo: 42 },
callable = Object.assign(function () {}, object);
console.log(callable)
console.log(callable())
console.log(Object.keys(callable));
You can't make an object callable.
You can add properties to an already existing Function
object though. You will still retain call()
behavior, but can also get and set properties.
var object = { increment: function(x){
return x+1
} ,
foo: 'foo'
};
var decrement = function(x){
return x-1
}
callable = Object.assign(decrement, object);
console.log(callable.increment(12)) //13
console.log(callable(67)) //66
console.log(Object.keys(callable));//[ 'increment', 'foo' ]
There are different prototypes
in javascript. E.g. Object.prototype
, Function.prototype
, Array.prototype
. Javsacript works based on prototypical inheritance
. This defines what is the type
of any object
.
When you create a function, its __proto__
(also called as "dunder proto") is set to Function.prototype
. Basically a function inherits from Function.prototype
which in turn inherits from Object.prototype
. Following example is taken from MDN. (Note - I think __proto__
is not standardized. Object.getPrototypeOf
should be used)
function f() {
return 2;
}
// Functions inherit from Function.prototype
// (which has methods call, bind, etc.)
// f ---> Function.prototype ---> Object.prototype ---> null
MDN has a great explanation for prototypical inheritance
. For more information read this https://developer.mozilla/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain
本文标签: How can an object become callable like a function in JavascriptStack Overflow
版权声明:本文标题:How can an object become callable like a function in Javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741217196a2360274.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论