mirror of
https://github.com/flynx/Course-JavaScript.git
synced 2025-10-28 18:40:08 +00:00
reworking type docs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
4d24787244
commit
0c922146f6
24
js-oop.js
24
js-oop.js
@ -172,6 +172,30 @@
|
|||||||
|
|
||||||
var a = new A()
|
var a = new A()
|
||||||
|
|
||||||
|
// The above aproach has one rather big flaw -- if called without new
|
||||||
|
// it will modify the root contect (i.e. window or global)
|
||||||
|
// A safer way to define a constructor:
|
||||||
|
|
||||||
|
function A(){
|
||||||
|
return {
|
||||||
|
__proto__: A.prototype,
|
||||||
|
x: 1,
|
||||||
|
y: 2,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var a = new A()
|
||||||
|
var a = A()
|
||||||
|
|
||||||
|
// XXX a way to check if we are running in a new context...
|
||||||
|
function A(){
|
||||||
|
var obj = this instanceof A ?
|
||||||
|
this
|
||||||
|
: { __proto__: A.prototype }
|
||||||
|
obj.x = 1
|
||||||
|
obj.y = 2
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|
||||||
// Some terminology:
|
// Some terminology:
|
||||||
// - in the above use-case 'A' is called a constructor,
|
// - in the above use-case 'A' is called a constructor,
|
||||||
|
|||||||
81
js-types-n-oop.js
Normal file
81
js-types-n-oop.js
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
|
||||||
|
/**********************************************************************
|
||||||
|
*
|
||||||
|
* JavaScript types and objects
|
||||||
|
*
|
||||||
|
*
|
||||||
|
**********************************************************************/
|
||||||
|
|
||||||
|
|
||||||
|
// Basic values
|
||||||
|
// ============
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Objects
|
||||||
|
// =======
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
// Prototypes and inheritance
|
||||||
|
// --------------------------
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
// Constructors
|
||||||
|
// ------------
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
|
||||||
|
/**********************************************************************
|
||||||
|
* vim:set ts=4 sw=4 : */
|
||||||
Loading…
x
Reference in New Issue
Block a user