summaryrefslogtreecommitdiffstats
path: root/jsnotes.txt
blob: ea4a049caae0c857458ad1c7241ef7800ab23cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
JS...
- JS includes keyval as language construct (aka associative array/hashtable/dictionary),
  this brings confusion and multiple ways to do 1 thing (as objects, arrays, and functions can be interchangeable)
- everything is an object, if that wasn't enough, JS is reflective,
  at any point we may add/mutate properties of object e.g. foo.newmember = "bar";

Best practices / rule of thumb
- when in doubt how to store data, always start with JSON (don't sore functions)
- always use 'var' keyword unless you're dereferencing a global

Functions
- all functions are objects
- 'this' keyword can be used in function if it is being new'ed (as in creating object/instance),
  if function was called without 'new' then this.foo will bind to window.foo ...
- NEW keyword, JS expects a constructor when new'ing, this implies we should NEW a function,
  new'ing e.g. 'new {} throws TypeError: object is not a function'


[references]
http://msdn.microsoft.com/en-us/library/ie/d1et7k7c%28v=vs.94%29.aspx

[data types]
- built-ins?
    the keval/dictionary/hash/associate-array language construct
- primitive
  string, number boolean

- composite (reference)
  object, array

  rvalues: new Object(), Object(), function() {}, {}
  rvalues: [], new Array(), Array()

- special
  null, undefined
  note: null is not 0, does not have type, undefined is a string

[scope]
- var keyword is used to make local variables

[anonymous functions]
wrapping code inside of 'function() { code } ()' is handy for creating a
namespace for your code and restricting scope, since js essentially has
a global and function scope, we don't want to pollute and cause collisons

[closures]
in JavaScript, if you use the function keyword inside another function, you are
creating a closure. if a function containing function f returns that function,
that variable points to f and also to the closure, unlike C, where stack frame
would get destroyed, so f has access to variables/object in the context it was
defined in

- what about function returning a function
  that's a good way to leverage closures!

['this' and 'prototype' object and the 'new']
- every object has a prototype object
- the keyword 'this' refers to the object context within which the
  the function is executing
- protype is used for templating, it provides access to members of an object,
  when function is assigned to this object, only 1 copy exists, it is memory
  efficient, also we get expected behavior when overriding that function
- 'new' changes meaning of a function, upon invocation, function is ran in blank
  object context


[some examples]
> 4 - 2    is 2
> 4 - "2"  is 2
> "4" + 2  is "42"

> 0 == false  true
> 1 == "1"    true  (js
> 1 === "1"   false (no type coercion is taking place)

> [] + []     "" empty string
> [] + {}     {object Object] Object
> {} + []     0
> {} + {}     NaN


~~~~~----- BROWSER -----~~~~~

[JS in a browser]
- there are 3 main global objects
  1) window......is the execution context and global object for that context's JS
  2) document....contains the HTML
  3) screen......describes the physical display's full screen


- your code gets executed inside something that looks like

    with (window) {
        //Your code
    }

- console.log is not available in to use from within HTML->JS, however it does exist in browser's console (Ctrl+Shift+k or F12)

~~~~~----------~~~~~