I start this coming from the standpoint of an honest question that I
don't know the answer to: is there a specific reason that objects
can't be callable in js? Aside from the "that's just how the language
is" answer, I was wondering if there's some other computer sciency or
performance reason for the restriction.
I've had this question a number of times but the most recent spart was
seeing this from Allen Wirfs-Brock:
> //define a non constructible superclass that provides some Smalltalk-like
> conventions
> const AbstractClass = Function.prototype <| {
> subclassResponsibility() {throw new Error(this.name+" did not implemented
> an abstract method")},
> shouldNotImplement() {throw new Error(this.name+" should not implemented
> by "+this.name)},
> name: "AbstractClass",
> prototype: Object.prototype <|{
> get class() {return this.constructor},
> error(message) {throw new Error(message)},
> subclassResponsibility() {return this.class.subclassResponsibility()},
> shouldNotImplement() {return this.class.shouldNotImplement()},
> errorSubscriptBounds(index) {this.error("subscript is out of bounds:
> "+index)}
> }
> };
Currently if you wish to callables that have a useful inheritance tree
you have to use a function literal and then use `__proto__` to
manually set it up. Presumably this is fixed one way or another with
ES6 by whatever thing is decided on to implement classes but it still
brings up the fundamental question for me.
As it is currently, the behavior of a function is that it defaults to
be a `constructor-like`. It has a prototype that it bestows upon
objects JS tells it to create, and it's not even possible for it to
have no prototype property. I would imagine a callable object is more
like a non-constructor function (something like isNaN) or, currently,
it's essentially the same as doing `object.valueOf()`. In fact there's
nothing stopping one from making every single object they use (aside
from an array) callable simply by starting with a function literal and
assigning its __proto__.
This is similar to how it's also currently impossible to make an
arraylike that really acts like an array without using __proto__. In
this case the answer to my initial question could be something like
"the storage of an array's indexed properties in memory asserts
specific requirements on its usage that are different from standard
properties" or something like that. That doesn't seem to follow with a
callable object though, since it has an affect only on something which
is other wise not usable in JS (default functionality or whatever it
would be called).
Partilly this is resolved with ES6, as is the arraylikes problem. But
I still am left curious as to why an object, not just something with
Function.prototype in its prototype tree, shouldn't be callable.
JavaScript fashions functions to be constructors from the getgo but it
seems to me that there's two almost completely separate types of
`things` between constructors and the traditional concept of
"function". On reflection is almost seems like in prototypal
inheritance, calling an object is where the constructor should
actually be, and functions should non-creational collections of
instructions.
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss