Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

type or object

56 views
Skip to first unread message

transkawa

unread,
Nov 16, 2009, 10:36:32 AM11/16/09
to
sorry, am new to javascript and still reading the reference manuals. but i
have a confusion - when does one decide to use a type (data type) instead
of an object
and vice versa? it's all so confusing especially when one is used to
classes.
tnx
david

Richard Cornford

unread,
Nov 17, 2009, 12:23:51 PM11/17/09
to
On Nov 16, 3:36 pm, transkawa wrote:
> sorry, am new to javascript and still reading the reference
> manuals.

Which reference manuals?

> but i have a confusion - when does one decide to use
> a type (data type)

What do you mean by "type" and "data type"?

> instead of an object
> and vice versa?

That is going to depend on what you meant by "type" and/or "data
type".

> it's all so confusing especially when one is used to
> classes.

That seems reasonable as there are no classes in javascript (or there
is only one class, depending on how you want to look at), but that
does not prevent programming in javascript using the concept of
'class', and so much talk of 'classes' in relation to javascript code.

Richard.

Dmitry A. Soshnikov

unread,
Nov 17, 2009, 1:15:44 PM11/17/09
to

If you're thinking about "class" as classification, typification
there's no syntactic constructs in ES for creating own "types" (in
meaning types as classes). But in quotes combination of constructor
and prototype can be called (again - "in quotes") - "class" - like a
set of characteristics of the objects related to their classification.
That's the general theory regardless ECMA-262-3.

But in ES, if to speak about types, there're 9 types (see ECMA-262-3,
8. Types) and for exactly ECMAScript program only 6 are available:

- Undefined
- Null
- Boolean
- String
- Number
- Object

Rest three types are implementation specific and used only for
intermediate results (on implementation level):

- Reference
- List
- Completion

Backing to ECMAScript program types, first five of them are types of
*primitive* values. And the only one type is the type of complex
structure - Object type (don't confuse with Object constructor!).

Every value of an Object type has a [[Class]] internal property which
is used by specification for distinguish *classification*,
*typification* of the objects. It could be any value, some of them
are: "Object", "Array", "Number", "String" and so on.

To see the [[Class]] property is possible via
[Object.prototype.toString] method, which returns (if not overridden)
the following string: "[object " + [[Class]] + "]".

Every object inherits from [Object.prototype], so it's possible to use
it implicitly as own method:

alert(new Number(1)); // [object Number]
alert([]); // [object Array]
alert(new String("")); // [object String]

and so on.

Also possible to call [Object.prototype.toString] explicitly via
[call] or [apply] methods:

var a = 10;
alert(Object.prototype.toString.call(a)); // [object Number]

In the example above you can see that for primitive value [a] result
is "[object...". That's because of implicit *type conversion* (see
ECMA-262-3, 9. The Completion Type). And only thanks to implicit type
conversion it's possible to say "Everything in ES is an object",
indeed, we can say, "everything" is a primitive and object type is
only one ;)

Backing to combination of [constructor function + prototype] - it's
just the start pattern which will be used when will being created.
It's *not* a own classification or typification. The simple object is
created (just like new Object or {}), its [[Class]] property is
"Object". After that, constructor can be "killed" (by assigning e.g.
[null] to implicit and explicit references) and object will continue
to exist having meanwhile implicit reference to its prototype -
internal [[Prototype]] property, which can't be changed in current
version of specification.

That's only little part of OOP in ECMAScript, if you interesting in
"classification and typification", you should start to lean
constructors and prototypes.

/ds

0 new messages