On 23.10.2011 10:07, Axel Rauschmayer wrote:
Sorry for bringing this up again, but I’m still having trouble with naming things properly in JavaScript.

- What do you call something that produces instances in JavaScript? A class? A type? A constructor? Or is a constructor the implementation of a type?

In ES spec it's called precisely and exactly -- it's a constructor.

In contrast with other languages (usually with strong and static typing), in ES a concept of a "class" doesn't reflect the concept of a "type".

Thus, the "statics" vs. "dynamics" doesn't cancel the fact that a "class" can be equivalent of a "type" -- e.g. in Python, classes are first-class, but they are "types":

class A(object):
    pass

a = A()
type(a) # class A

type(a) == a.__class__ # true

The same picture can be seen in other languages (Java, C++, etc).

In JS an "entity" which generates instances isn't the class of the instances. This entity (once again, and that is mentioned in the spec) is called a constructor.

However, if you consider some generic aspects of computer science, then a pair of constructor function + prototype may be called a "class", since the work of class is (1) to generate instances by a template and (2) hierarchical (vertical) code reuse which is called inheritance. Constructor function + prototype pair does exactly this in ES. So it's a "class" (and can be syntactically sugared as in CoffeeScript and Python; yes, Python is delegation-based and classes there are just the same syntactic sugar). More precisely -- a "dynamic first-class class" (for more info on types of classes, see this: https://gist.github.com/977034)

So if you want to talk in borders of ES, call it constructor. If you call abstractly at comp.sci level, you may call it class. But in contrast with other langs to which you used to, a class here isn't a type.


- Are Date and RegExp built-in types? What do you call the value of the global variable Date? Is new Date() an instance of a type or an instance of a constructor?

Precisely Date and RegExps are constructors which generate instances of the Object type (don't confuse with the Object constructor!). You may call it (unofficially) sub-types of Object type.

An instance of a constructor. And all instances are of type of Object.


- What about primitives?Are there primitive types and object types?

Please read this one completely to cover these questions in detail and completely: http://dmitrysoshnikov.com/ecmascript/chapter-7-2-oop-ecmascript-implementation/

There is only one object type and it's called Object type. All the other types (excluding internal) are primitive types. A value gets the Object type when is created by any constructor. After that you may distinguish these values (which all have the Object type) only by analyzing prototype chain (via instanceof operator) or by testing "classification-tag" -- the inherited property `constructor` or (for standard "classes") by testing "classification-tag" -- the internal property [[Class]], which though is planned (by Allen) to be removed from the spec.

Is the union of the two called just “types”?


In comp.sci it's called a class which may be an equivalence of a type. Here two instances are just objects which were created by some constructor and, once again, can the same type Object. But, may have different prototype chain and classification-tags.

- If instance factory B inherits from instance factory A, is B a subclass of A? B a subtype of A? B a subconstructor of A?

Your assumption points that you very likely mostly programed before in static systems (such as Java, etc). Please refer this info to cover the topic completely: http://dmitrysoshnikov.com/ecmascript/chapter-7-1-oop-general-theory/

In ES, a "factory B" doesn't inherit from "factory A". Here only objects inherit from their prototype chains. A "factory A" inherits from its prototype chain (usually Function.prototype).

If you though want to use concept of a class and create a useful sugar (class system) for it and build your system using classes, then yes, you may say that class B inherits from class A -- and it will be correct an this abstraction level. In practice (i.e. on lower abstraction level), it will just mean that an object created by the constructor B inherits from the prototype which in turn inherits from the A.prototype.



- Does a class literal (if it makes it into ES.next) define a class? Is a class the implementation of a type?

No, in current ES type system a class will not define a type. However it can be fixed (e.g. to set [[Class]] property to constructor name and to take into account typeof operator). In ES, classes are just sugar for proto-based inheritance. The same as in Coffee, the same as in Python.



Intuitively, it is clear how things work, but I find it difficult to be precise when talking/writing about them.


Hope it helped.

More info you'll find again in the literature I gave above and in the spec (after reading the spec there will be no questions such as "is class is the type in ES?").

Dmitry.
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to