#p2pu-Jan2011-javascript101
In the first part [00:00 - 08:00 mins] of this video, Douglas Crockford talks about statements in Javascript. I have blogged my notes below:
JS has labelled breaks, nice to know.
The '
for' statement can be used with
arrays (as expected), but it can be used with
objects also. In the latter case it iterates through all the keys in an object, and it's ancestors.
for() {
if(object.hasOwnProperty(name)) {
}
}
As most other langauges Javascript also has a
switch statement, however the switch value does not need to be a number, and case values can be expressions.
We can
throw and catch Exceptions in Javascript. Since JS does not have classes, there is just one Exception (which is of type Object). The Javascript engine can throw the following Exception names (all of type Object) (Error, EvalError, RangeError, SyntaxError, TypeError, URIError)
try {} catch(e){}
Do not use the with statement...
Javascript supports
functions. All functions start with the function keyword.
function name(parameters) {
}
Javascript uses the keyword
var to declare variables.
var student;
If a var is not initialized then the variable will be set to undefined. Javascript has implicit global variables.
In JS blocks do not have scope, only function scope. We can declare a variable anywhere in a fuction (even after it's use?). Remember if we declare a variable twice, it gets created only once, and no error is generated.
Every function in Javascrpt returns something. If we do not explicitly return anything, then 'undefined' will be returned.
Javascript's switch statement is less limiting than C or C++. In Javascript the value in the switch part is not restricted to numbers. It can contain numbers, Strings, or any object for that matter. The case statements are also not limited to having only constants. They can even contain expressions.