At line 11 changed one line |
| String | 'Hello' |
| String | 'Hello' (prefer this form) |
At line 18 added one line |
| NaN | not a number |
At line 20 added 2 lines |
| Object (JSON) | {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"} |
| Array | {{{[33, 'Dog', 55.6]}}} |
At line 40 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 59 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 79 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 145 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()); |
|
}}} |