2023-06-01 16:53:25 +03:00
|
|
|
/**********************************************************************
|
|
|
|
|
*
|
|
|
|
|
* JavaScript types and objects
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
**********************************************************************/
|
2023-06-02 15:27:00 +03:00
|
|
|
//
|
2023-06-03 23:04:56 +03:00
|
|
|
// Types and objects
|
|
|
|
|
// =================
|
2023-06-02 15:27:00 +03:00
|
|
|
//
|
|
|
|
|
// JavaScript's type system is split into two categories of enteties:
|
|
|
|
|
// basic types or values and objects, they differ in several aspects.
|
|
|
|
|
//
|
|
|
|
|
// | values | objects
|
|
|
|
|
// ----------------+-------------------+--------------------------
|
|
|
|
|
// mutability | imutable | mutable
|
|
|
|
|
// ----------------+-------------------+--------------------------
|
|
|
|
|
// identity | equal values are | different objects
|
|
|
|
|
// | the same entity | can have same
|
|
|
|
|
// | (singletons) | structure
|
|
|
|
|
// ----------------+-------------------+--------------------------
|
|
|
|
|
//
|
|
|
|
|
//
|
2023-06-01 16:53:25 +03:00
|
|
|
// Basic values
|
2023-06-03 23:04:56 +03:00
|
|
|
// ------------
|
2023-06-01 16:53:25 +03:00
|
|
|
//
|
|
|
|
|
// numbers
|
|
|
|
|
var integer = 123
|
|
|
|
|
var floating_point = 3.1415
|
|
|
|
|
var hex = 0xFF
|
|
|
|
|
|
|
|
|
|
// strings
|
|
|
|
|
var string = 'string'
|
|
|
|
|
var another_string = "also a string"
|
|
|
|
|
var template = `
|
|
|
|
|
a template string.
|
|
|
|
|
this can include \\n's
|
|
|
|
|
also summorts expansions ${ '.' }`
|
|
|
|
|
|
|
|
|
|
// boolieans
|
|
|
|
|
var t = true
|
|
|
|
|
var f = false
|
|
|
|
|
|
|
|
|
|
// nulls
|
|
|
|
|
var n = null
|
|
|
|
|
var u = undefined
|
|
|
|
|
var not_a_number = NaN
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Values are in general:
|
|
|
|
|
//
|
|
|
|
|
// - singletons
|
|
|
|
|
var a = 3.14
|
|
|
|
|
var b = 3.14
|
|
|
|
|
a === b // -> true
|
|
|
|
|
|
|
|
|
|
// In general equal basic values are the same value and there is
|
|
|
|
|
// no way to create two copies of the same value.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// - imutable
|
|
|
|
|
var a = 1
|
|
|
|
|
var b = a
|
|
|
|
|
|
|
|
|
|
// a and b hold the same value (1)
|
|
|
|
|
a === b // -> true
|
|
|
|
|
|
|
|
|
|
// now we update a...
|
|
|
|
|
a += 1
|
|
|
|
|
a === b // -> false
|
|
|
|
|
// Note that we updated the value referenced by a, i.e. the old
|
|
|
|
|
// value (1) was not modified by the addition (b is still 1),
|
|
|
|
|
// rather a new value (2) was created and assigned to a.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-06-02 17:00:33 +03:00
|
|
|
// Equality and identity
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
2023-06-04 23:16:10 +03:00
|
|
|
// Automatic type coercion
|
|
|
|
|
//
|
|
|
|
|
|
2023-06-02 17:00:33 +03:00
|
|
|
|
2023-06-05 23:24:06 +03:00
|
|
|
// Type checking
|
2023-06-03 23:04:56 +03:00
|
|
|
//
|
2023-06-06 19:18:04 +03:00
|
|
|
typeof(42) // -> 'number'
|
2023-06-07 23:58:52 +03:00
|
|
|
typeof('meaning of life') // -> 'string'
|
2023-06-03 23:04:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-06-01 16:53:25 +03:00
|
|
|
// Objects
|
2023-06-03 23:04:56 +03:00
|
|
|
// -------
|
2023-06-01 16:53:25 +03:00
|
|
|
//
|
2023-06-03 23:04:56 +03:00
|
|
|
|
|
|
|
|
// Type cheking
|
2023-06-01 16:53:25 +03:00
|
|
|
//
|
2023-06-06 19:18:04 +03:00
|
|
|
// Here thesame approach as for simple types is not productive:
|
|
|
|
|
|
|
|
|
|
typeof([42]) // -> 'object'
|
|
|
|
|
typeof({}) // -> 'object'
|
|
|
|
|
|
|
|
|
|
// so a better approach would be to:
|
|
|
|
|
|
|
|
|
|
[42] instanceof Array // -> true
|
|
|
|
|
|
|
|
|
|
// but since all objects are objects the test can get quite generic (XXX)
|
|
|
|
|
|
|
|
|
|
[42] instanceof Object // -> true
|
|
|
|
|
{} instanceof Object // -> true
|
|
|
|
|
|
|
|
|
|
|
2023-06-01 16:53:25 +03:00
|
|
|
|
2023-06-03 23:04:56 +03:00
|
|
|
|
2023-06-01 16:53:25 +03:00
|
|
|
// Prototypes and inheritance
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// Constructors
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
|
* vim:set ts=4 sw=4 : */
|