Tag Archives: Scripting languages

JavaScript Objects Coding Style

JavaScript vs. PHP

Continuing from my post about PHP arrays coding style and following the comments of that post, I’d like to write a bit about JavaScript objects’ coding style.

You perhaps know that the term object is quite undefined or under estimated in the JavaScript world, but I’d speak about the key/value pairs in JS commonly formatted like that:

var obj = { key : 'value' }

Here you can add more and more key/value pairs, but what’s different from the PHP associative arrays and what’s the same and should be cosidered.

The Same as PHP?

I wrote about the alignment in PHP and hashes. Than I showed how I align them:

$arr = array(
   'short'   => 'val',
   'longkey' => 'val'
);

In JavaScript you should use the same technique of alignment:

var obj = {
   'short'   : 'val',
   'longkey' : 'val'
};

Some Differences

Yes, there are more differences, which is normal. First of all you don’t have the => notation in JavaScript and a : is used. Second and most important you cannot add a trailing comma after the last key/value pair. Note that in PHP that’s fine!

// that will throw an error in MSIE
var obj = {
   'short'   : 'val',
   'longkey' : 'val',
};

while this is OK in PHP and it’s encouraged:

$arr = array(
   'short'   => 'val',
   'longkey' => 'val',
);

PHP: The Array Element Doesn’t Exist – Suppress the Warnings

What’s the problem?

If you code PHP from couple of hours you are probably familiar with the following problem:

1
2
3
4
5
6
7
8
9
<?php
 
	error_reporting(E_ALL);
 
	$arr = array();
 
	echo $arr[3];
 
?>

The first and most common solution is just by adding the isset() statement and the code will look like that:

1
2
3
4
5
6
7
8
9
10
<?php
 
	error_reporting(E_ALL);
 
	$arr = array();
 
        if (isset($arr[3]))
	        echo $arr[3];
 
?>

However do you know there’s is another simple and elegant solution? Simply add the @ sign to suppress the element existence

1
2
3
4
5
6
7
8
9
<?php
 
	error_reporting(E_ALL);
 
	$arr = array();
 
	echo @$arr[3];
 
?>

Inline Scripts with Zend_View_Helper_InlineScript

Once I’ve posted about Zend Framework and script injections into the view of the app. However back than I didn’t mentioned the way the scripts can be injected on whatever place into the markup. This job’s done by Zend_View_Helper_InlineScript and that’s only an abstraction over the HeadScript.

However thus the PHP code goes quite clean and maintainable.

$scripts = $this->view->inlineScript();
$scripts->appendFile('/scripts/production.js');

This is quite interesting because in the case of multiple scripts you can chain them into this:

$scripts = $this->view->inlineScript();
$scripts->appendFile('/scripts/production.js')
            ->appendFile('/scripts/development.js')

But that’s not everything. Although it looks very pretty and clean, PHP gives you the correct syntax of something like this:

$scripts = $this->view->inlineScript();
$scripts
            ->appendFile('/scripts/production.js')
            ->appendFile('/scripts/development.js')

And that’s particularly good when it comes to fast switching between production and development scripts includes.

$scripts = $this->view->inlineScript();
$scripts
 //        ->appendFile('/scripts/production.js')
            ->appendFile('/scripts/development.js')

You’d ask why I’d to comment my script includes. Because as it appears to be fashionable the JavaScripts are concatenated and compressed. This gives you performance benefits on the client side when downloading and executing the script. So it’s usual to have one compressed/minified and several development scripts. That’s why this commenting strategy is very useful.

Should I do Something Only Because Crockford Says So?

Crockford says: “Don’t do that, please stop doing it!?” It may be related to JavaScript, YUI, CSS or whatever. PPK recently posted about browser prefixes with the same intonation. OK, fine, it’s difficult to maintain, it’s ugly but my job is to make the site look as nice as possible.

I agree with Crokford that only with copy/paste now JavaScript has the reputation of badly standardized language with no programmers that understand it at all. It’s true, but however this maybe made JavaScript so popular, now all the libraries come with that abstraction in mind and this gap seems to be smaller.

In addition I’ll say that’s fine if Crockford or PPK say something like that, but the primary goal of every web developer is not to be in good relations with Crockford, but to satisfy clients! I’d like to repeat – clients!

Modyfing Response Headers with Zend Framework

Response Headers

You’ve probably seen that you can track the server response headers with Firebug – a wonderful extension to Firefox.

But have you ever asked yourself how to change a response header with Zend Framework.

header()

Typically this is done with the built in PHP function header, where you can setup the header the page must return, but how to make it with Zend Framework?

It’s pretty simple of course, it’s Zend Framework in fact. To be more precise I’ll post a snippet:

$response = $this->getResponse();
$response->setHeader('content-type', 'application/x-javascript; charset=utf-8');
$response->sendResponse();