15. var
• Declares a variable
• If you forget, it will be defined globally!
• Scope of variable is the function
Tuesday, November 9, 2010
16. Conditionals
• If
• Switch - Case
if (unit == 'Litre')
return value;
else
return this.withTwoDecimals(value / 3.7854);
Tuesday, November 9, 2010
17. Loops
• while
• do - while
• for(var i; i<42; i++)
• for (key in object)
for (var key in market) {
current.market[key] = gui.convert(market[key]);
}
Tuesday, November 9, 2010
43. Arrays
• Array inherits from object
• Indexes are converted to strings
• Magic length property
• Always one larger than the highest int
property
Tuesday, November 9, 2010
60. Scope
The function is the scope of the
variables
Tuesday, November 9, 2010
61. Invocation Forms
• Function form
• sleep(10)
• Method form
• kjell.sleep(10)
• kjell[“sleep”](10)
• Constructor form
• new sleep(10)
• Apply form
• sleep.apply(rudolph, 10)
Tuesday, November 9, 2010
62. this
Invocation this
this is an extra Form
parameter that the global
function
object
depends on the
calling form method kjell
constructor a new object
apply rudolph
Tuesday, November 9, 2010
63. arguments
• A special array like, DYNAMIC, parameter
• All the arguments of the invocation
Tuesday, November 9, 2010
64. function sum() {
var i,
n = arguments.length,
total = 0;
for (i = 0; i < n; i += 1) {
total += arguments[i];
}
return total;
}
sum(1, 2, 3, 4);
Tuesday, November 9, 2010
65. Dynamic Compilation
• eval
• Evaluates a string and returns the result.
• new Function(parameterArray, codeString)
• Creates and returns a function.
• var add=new Function("a", "b", "return a+b;");
Tuesday, November 9, 2010
67. The global Object
• Container for all variables
• On browsers window == global
• Any var not declared is global
• Global variables are BAD
Tuesday, November 9, 2010
69. Modules
var MyNamespace = {};
var MyNS = MyNS || {};
Tuesday, November 9, 2010
70. Cascade
setFirst: function(name) {
this.first = name;
return this;
}
person
.setFirst(“Anders”)
.setLast(“Janmyr”)
.setAge(40);
Tuesday, November 9, 2010
71. Encapsulation
var Esperanto = Esperanto || {};
Esperanto.Lab = function() {
var privVar = "example";
function privFunc() {
return privVar;
}
return {
example: function() {
return privFunc();
}
}
}()
Tuesday, November 9, 2010
72. Local Functions
costData: function(current) {
var data = {};
function addEntry(name, cost) {
data[name + "PerHour"] = model.withTwoDec(cost/hours);
data[name] = model.noDecimalsWithSeparator(cost);
};
addEntry("interest", this.financialCost(current)),
addEntry("depreciation", this.depreciationCost(current)),
return data;
},
Tuesday, November 9, 2010
73. self = this
attachFormListener: function(form, object) {
var self = this;
function populator(event) {
self.populateFromForm(form, object);
object.notify();
};
form.getElements().each(function(child) {
child.observe('change', populator);
});
},
Tuesday, November 9, 2010
74. Mixins
Object.mixin = function(destination, source) {
for (property in source)
destination[property] = source[property];
return destination;
}
Tuesday, November 9, 2010
75. Enforcing New
function User(first, last) {
if ( !(this instanceOf User))
return new User(first, last);
this.name = first + ‘ ‘ + last;
}
Tuesday, November 9, 2010