At line 7 added 2 lines |
All symbols in JavaScript are case-sensitive. |
|
At line 11 added 11 lines |
| String | 'Hello' (prefer this form) |
| | "Hello" |
| Number | 45 |
| | 3.14 |
| Boolean | true/false |
| null | null |
| undefined | undefined |
| NaN | not a number |
| RegExp | /.*/g |
| Object (JSON) | {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"} |
| Array | {{{[33, 'Dog', 55.6]}}} |
At line 23 added one line |
|
At line 12 changed one line |
No character type |
There is no character type. |
At line 14 changed one line |
Strings are represented with double-quotes OR single-quotes, i.e. |
Strings are represented with double-quotes OR single-quotes, and characters can be escaped with a backslash. |
At line 16 removed 12 lines |
{{{"Hello" |
'there'}}} |
|
Single-quote strings can embed double-quotes, and double-quote strings can embed single-quotes, i.e.: |
|
{{{"don't you like this string" |
'var abc = "value"'}}} |
|
You can also use escapes "\", i.e.: |
|
{{{'don\'t you like this'}}} |
|
At line 33 changed one line |
! Boolean values and Null |
! Boolean values and null |
At line 41 changed one line |
"null" is also a constant that means no value |
false, null, undefined, NaN, 0, and "" are all treated as "false". Anything else is treated as "true". |
At line 45 added 2 lines |
The {{{===}}} operator is used to tell if the two arguments are the same object (not just equal). |
|
At line 49 added one line |
Variable names must start with a letter, underscore, or $, and may contain all of those characters plus numbers thereafter. |
At line 57 changed one line |
Variables are local to the context in which they are declared. This means if you declare it outside of a function, it is global. If you declare it inside a function it is local to that function. Keep all variables as local as possible. |
Variables are either global or local to the function in which they are declared. This means if you declare it outside of a function, it is global. If you declare it inside a function it is local to that function. Keep all variables as local as possible. {} blocks do not establish a variable context / scope in JavaScript. |
At line 77 changed one line |
Another way to create a function is through a variable syntax. Although you may think "what good is that?", it is extremely important, and it opens the door to some very, very important things. The above function can also be defined as follows: |
Another, equivalent, way to create a function is through the variable syntax. Although you may think "what good is that?", it is extremely important, and it opens the door to some very, very important things. The above function can also be defined as follows: |
At line 143 changed one line |
! Object |
! Objects |
At line 202 added one line |
!! Classes |
At line 204 added 5 lines |
{{{ |
function MyClass(init) { |
this.iv1 = init; |
this.iv2 = 0; |
} |
At line 210 added one line |
MyClass.cv1 = 101; |
At line 212 added 66 lines |
MyClass.getCV1 = function() { |
return MyClass.cv1; |
} |
|
MyClass.setCV1 = function(val) { |
MyClass.cv1 = val; |
} |
|
MyClass.prototype.getIV1 = function() { |
return this.iv1; |
} |
|
MyClass.prototype.setIV1 = function(val) { |
this.iv1 = val; |
} |
|
|
var x = new MyClass(36); |
var y = new MyClass(42); |
|
print(x.getIV1()); |
print(y.getIV1()); |
print(MyClass.cv1); |
|
x.setIV1(44); |
y.setIV1(22); |
MyClass.setCV1(202); |
|
print(x.getIV1()); |
print(y.getIV1()); |
print(MyClass.getCV1()); |
}}} |
|
|
|
! Inheritance |
|
{{{ |
function Mammal(name) { |
this.name = name; |
} |
|
Mammal.prototype.getName = function() { |
return this.name; |
} |
|
function Cat(name, color) { |
Mammal.call(this, name); // call the super constructor to inherit its instance vars |
this.color = color; |
} |
|
Cat.prototype = Object.create(Mammal.prototype); // copy parent prototype to |
// inherit its methods |
// this must be done before |
// assigning local methods |
|
Cat.prototype.getColor = function() { |
return this.color; |
} |
|
var a = new Cat("Woolfy", "Brown"); |
|
print(a.getName()); |
print(a.getColor()); |
|
}}} |