on-internet
← Community

What is the difference between a function and an object literal?

Stack Overflow ·

In JS functions belong to the object type. Objects can be converted to primitive types with [Symbol.toPrimitive](hint). This pure-object conversion works fine: let obj = { [Symbol.toPrimitive](hint) { switch(hint) { case "string": return "all right"; case "number": return 100; default: return "something else"; } } }; alert(obj); // all right alert(+obj); // 100 However, something is wrong with function conversion: function fun() { [Symbol.toPrimitive](hint) { // JS engine starts to complain here switch(hint) { case "string": return "all right"; case "number": return 100; default: return "somet

Comments

Sign in or sign up to comment and upvote.

No comments yet.