Implicit Javascript Variables

December 5, 2007 § Leave a comment

These are javascript variables that I often forget existed because they are implicit but nonetheless very useful.

I’m putting them here just so that I won’t forget anymore. Hope readers found them useful as well.

Arguments, it is a local variable exists in all functions. Based on MozDev documentation, arguments object is not an array. It is similar to an array, but does not have any array properties except length. For example, it does not have the pop method. However it can be converted to array object via:

var args = Array.prototype.slice.call(arguments);

Arguments object is available only within a function body. Attempting to access Arguments object outside a function declaration results in an error.

Function Names, when creating a function using “function declaration”, for example:

function iamawesome() { return 5; }

a variable called iamawesome is created implicitly. Thus, you can pass around function as a variable (callback to be exact).


References:

MozDev Functions:Arguments

Function constructor vs Function declaration vs Function expression

December 2, 2007 § Leave a comment

This is 1 topic that I usually take for granted but never really quite “gets” it.

A function can be defined with Function Constructor assigned to variable. Example:

var multiply = new Function(“x”, “y”, “return x * y;”);

Function Declaration is typically used to define a function. Example:

function multiply(x, y) {

return x * y;

}

Function Expression, without name, can also be used to define a function to variable. Example:

var multiply = function(x, y) {

return x * y;

}

Function Expression can also be assigned a name. Example:

var multiply = function func_name(x, y) {

return x * y;

}

What exactly the differences between all those? Not much. But there are quite a number of trivia that I need to remember from now on.

Function Constructor (i.e. new Function(a, b, c)) is parsed EVERY TIME javascript is evaluated. Bad! Real bad! In contrast, Function Declaration and Function Expression are only parsed 1 time.

Obviously function name defined by Function Expression/Function Declaration cannot be changed, while the variable, to which the function is assigned to, can be manipulated/reassigned.

Function name defined by Function Expression can be used only inside the function body. Meanwhile, Function Declaration CREATES a variable with the same name as the function name. Thus, unlike Function Expression, Function Declaration can be accessed by its name in the scope they were defined in. Example:

var y = function x() {};

alert(x); // throws an error because the name “x” is not accessible outside the function

function x() {}

alert(x); // outputs x serialized into a string. It works.

Function Declaration automatically becomes Function Expression when it is defined inside conditional loop.

Trivia:

  • Every function contains Arguments array-like object.

Reference:

Mozilla Development Center – Functions

Awesome SQLAlchemy Tutorials

November 15, 2007 § Leave a comment

Below are couple of awesome SQLAlchemy tutorials,

the list will grow as I find more of these:

PHP: Quick tip on setting up server-side logger

October 30, 2007 § Leave a comment

Changes in php.ini:

  1. Around line:416: uncomment error_log variable, and assign it to your prefered logfile.txt. For example, on Windows box: error_log = “C:\log.txt”. Beware: make sure that PHP has permission to modify the log file on *NIX system.
  2. If you want to NOT displaying PHP error on the screen: Around line 360, make sure that: display_errors = off

Grunt work required to make sure that things are logged properly:

use if block:
if(something->bad->going->to->happen)
{
//call this system function
error_log(‘the message why this is bad’);
}

use try catch:
try
{
//do something that’s possibly bad
}
catch(Exception $e)
{
error_log(‘Exception thrown: ‘.$e->getMessage());
}

Javascript: setTimeout() vs setInterval()

October 11, 2007 § 1 Comment

What is setTimeout():

It is a function that can execute other javascript statement AFTER x interval. The interval unit is millisecond.

Syntax:

setTimeout(“do.something();”, 1000); //Execute do.something() 1 second later.

What is setInterval():

It is a function that can execute other javascript statement EVERY x interval. The interval unit is millisecond.

Syntax:

setInterval(“do.somethingElse();”, 2000); //Execute do.somethingElse() every 2 seconds.

Notes:

Use clearTimeout() to cancel setTimeout(). Similarly, clearInterval() is used to cancel setInterval().

When calling “this” inside setTimeout() or setInterval(), it refers to window(global) as opposed to the object you are calling the function from.

References:

CakePHP: book for newbies…

July 30, 2007 § 1 Comment

is almost here.

David Golding is the author, and here is his announcement about it.

Update: December 29th, 2007

Couple of the chapters are available here.

There’s also discussion forum about the book.

If you cannot wait to get started on CakePHP, see Resources below:

CakePHP: getting list of all of your controllers

July 30, 2007 § Leave a comment

This tips is written by Felix Geisendörfer aka the_undefined. He is waaaaay involved with CakePHP community.

His blog is recommended for readers who enjoy baking with CakePHP.

Where Am I?

You are currently browsing the tutorial category at RAPD.

Follow

Get every new post delivered to your Inbox.