Javascript Basics Part II


Statements

  • break
  • for
  • switch
  • throw
  • try
  • with

For statement

for (var name in object)
{
	if(object.hasOwnProperty(name))
	{
		// within the loop, name is the key of current memeber
		// object[name] is the current value
	}
}

Switch statement

  • The switch value can be a string. Not restricted to integer
  • The cast statements can be expressions, not just constants

Throw statement

throw new Error(reason);

throw 
{
	name: exceptionName,
	message: reason
}

Try statement

  • Because we don’t have classes, we just have one catch clause. There cannot be multiple catch clauses.
  • The javascript implementation can produce these exception names:
    • Error
    • EvalError
    • RangeError
    • SyntaxError
    • TypeError
    • URIError
try
{

}
catch(e)
{

}

With statement

  • Ambiguous and error prone, don’t use it.
with (o)
{
	foo = null;
}

Either o.foo = null; or foo = null; depending on if foo is a global variable

Function statement

function name(parameters)
{

}

Var statement

  • Types are not specified
  • Initial values are optional

Scope

  • Blocks do not have scopes, only function has scopes.
  • Because of laziness while writing the compiler :)
  • This is a mistake in language

Return statement

return expression; Or return;

  • If there is no expression, then the return value is undefined
  • Exception in constructors, return value is this