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-10 14:59:13 +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-08 22:17:35 +03:00
|
|
|
// this essentially checks if the left oprtand is related to (i.e. in the
|
2023-06-09 16:50:19 +03:00
|
|
|
// inheritance chain of) the second operand's .prototype, or we can say
|
2023-06-08 22:17:35 +03:00
|
|
|
// that it id "inherited" from the constructor.
|
2023-06-06 19:18:04 +03:00
|
|
|
|
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
|
|
|
|
|
//
|
2023-06-08 22:17:35 +03:00
|
|
|
|
2023-06-09 16:50:19 +03:00
|
|
|
var a = {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var b = Object.create(a)
|
|
|
|
|
|
|
|
|
|
var c = {
|
|
|
|
|
__proto__: b,
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-01 16:53:25 +03:00
|
|
|
|
|
|
|
|
// Constructors
|
2023-06-13 23:41:18 +03:00
|
|
|
//
|
|
|
|
|
// A constructor is simply a function that "constructs" or populates an
|
|
|
|
|
// object.
|
|
|
|
|
//
|
|
|
|
|
// By convention constructor functions are capitalized (Pascal-case)
|
|
|
|
|
//
|
|
|
|
|
// Classic constructors are called with a "new" keyword which creates a
|
|
|
|
|
// bare instance and passes it to the function as the call context.
|
2023-06-01 16:53:25 +03:00
|
|
|
//
|
2023-06-08 22:17:35 +03:00
|
|
|
|
2023-06-09 16:50:19 +03:00
|
|
|
function A(){
|
|
|
|
|
this.attr = 42
|
|
|
|
|
this.method = function(){
|
|
|
|
|
console.log('Hello world!')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var x = new A()
|
|
|
|
|
|
|
|
|
|
var y = {
|
|
|
|
|
__proto__: A.prototype,
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 14:59:13 +03:00
|
|
|
// XXX a safer way -- now we can forget new...
|
2023-06-09 16:50:19 +03:00
|
|
|
|
|
|
|
|
function B(){
|
|
|
|
|
var obj = {
|
|
|
|
|
__proto__: B.prototype,
|
|
|
|
|
}
|
|
|
|
|
return obj
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// this can be calles with and withot new
|
|
|
|
|
var z = B()
|
|
|
|
|
|
|
|
|
|
// less naive...
|
|
|
|
|
function C(){
|
|
|
|
|
var obj = this instanceof C ?
|
|
|
|
|
this
|
|
|
|
|
: { __proto__: C.prototype }
|
|
|
|
|
return obj
|
|
|
|
|
}
|
2023-06-10 14:59:13 +03:00
|
|
|
// make C instances related to B...
|
|
|
|
|
C.prototype.__proto__ = B.prototype
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Extending builtin types
|
|
|
|
|
//
|
2023-06-09 16:50:19 +03:00
|
|
|
|
2023-06-12 10:58:05 +03:00
|
|
|
// Mixing builtin types
|
|
|
|
|
//
|
|
|
|
|
// In general this is impossible in JavaScript due to the lack of any
|
|
|
|
|
// mechanism of horizontal name resolution in the inheritance chain like
|
|
|
|
|
// multiple inheritance (hence why we call it a chain and not a tree).
|
|
|
|
|
//
|
|
|
|
|
// So there is no way, for example, to make something both an array and
|
|
|
|
|
// a function at the same time.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-06-10 14:59:13 +03:00
|
|
|
// XXX Reflect.construct(Function, args, newConstructor)
|
|
|
|
|
// mainly usefull if the resulting instance has to be of a builtin
|
|
|
|
|
// type like a function (callable) or an array...
|
2023-06-11 23:27:52 +03:00
|
|
|
// ...especially when overloading the constructor
|
|
|
|
|
// XXX should this be in advanced topics???
|
2023-06-10 14:59:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Classes and JavaScript
|
|
|
|
|
//
|
|
|
|
|
// Since the class syntax is simply a more restrictive way to do the
|
|
|
|
|
// same as the above, in addition to introducing more "the same but
|
|
|
|
|
// slightly different" ways to define functions and methods thus adding
|
|
|
|
|
// lots of new details, pitfalls and nuances that give us essentially
|
|
|
|
|
// the same functionaly that already existed in the language with
|
|
|
|
|
// the onus of additional complexity, we will be completely ignoring
|
|
|
|
|
// them in this document.
|
2023-06-09 16:50:19 +03:00
|
|
|
|
2023-06-01 16:53:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
|
* vim:set ts=4 sw=4 : */
|