Tải bản đầy đủ - 0 (trang)
#9. Advanced JavaScript: closures

#9. Advanced JavaScript: closures

Tải bản đầy đủ - 0trang

www.it-ebooks.info

leftovers



#9. Advanced JavaScript: closures (continued)

The tmp variable is declared in local scope, inside the func1 function. This will always alert

16, because func2 can access the x (which was defined as an argument to func1), and it can

also access tmp from func1.

That is not a closure. A closure is when you return the inner function. The inner function will

close over the variables of func1 before leaving.

Now consider:

function func1(x) {



var tmp = 3;



return function (y) {



alert(x + y + (++tmp));



}

}

var func2 = func1(2); // func2 is now a closure.

func2(10);

Again, tmp is in the local scope, but the func2 function is in the global scope. The above

function will also alert 16, because func2 can still refer to x and tmp, even though it is no

longer directly inside the scope.

However, since tmp is still hanging around inside func2’s closure, it is also being incremented.

It will be incremented each time you call func2.

It is possible to create more than one closure function, either by returning a list of them or by

setting them to global variables. All of these will refer to the same x and the same tmp; they

don’t make their own copies.



#10. Templates

jQuery templates are still in beta, but are a cool, upcoming feature that might help you build

a more flexible site, without much HTML or jQuery. They are designed to take data and

bind it to some template markup, so you can consistently use the same markup to display

similarly related data.

Check them out here: http://api.jquery.com/category/plugins/templates/.



you are here 4   459



www.it-ebooks.info



www.it-ebooks.info



appendix ii: set up a development environment



Get ready for the big times

If I learn this early, I'll

be way ahead of everyone

else...



You need a place to practice your newfound PHP skills

without making your data vulnerable on the Web.  It’s

always a good idea to have a safe place to develop your PHP application

before unleashing it on the world (wide web). This appendix contains

instructions for installing a web server, MySQL, and PHP to give you a safe

place to work and practice.



this is an appendix   461



www.it-ebooks.info

installing php & mysql locally



Create a PHP development environment

Before you can put your finished application on the Web with your

newfound jQuery and AJAX skills, you need to develop it. And it’s never

a good idea to develop your web application on the Web where everyone

can see it. You can install software locally that lets you build and

test your application before you put it online.

There are three pieces of software you’ll need on your local computer to

build and test PHP and MySQL applications:

1. A web server

2. PHP

3. A MySQL database server

PHP isn’t a server; it’s a set of rules that your web server understands

that allow it to interpret PHP code. Both the web server and the MySQL

server are executable programs that run on a computer.

Keep in mind that we’re talking about setting up your local computer as

a web server for PHP development. You’ll ultimately still need an online

web server to upload your finished application to so that other people

can access and use it.



Web server software such as

Apache or IIS is required to

serve up PHP scripts as web

pages.



The MySQL database

server is often installed

on the same computer

as the web server

software—in this case,

your local computer!



Server computer



Web ser ver

Database server



In a PHP development

environment, your local

computer acts as a server

computer for the purposes

of running PHP scripts.



PHP is installed as part

of the web server and

allows the web server

to run PHP scripts.



Find out what you have

Before trying to install any of the pieces of the PHP development puzzle,

your best bet is to first evaluate what you already have installed. Let’s take

a look at the three pieces and how you can tell what’s already on your

system.

The platform of your local computer makes a big difference when it

comes to what’s already installed. For example, Mac OS X has a web

server installed by default, while most Windows computers do not.

462   appendix ii



NOTE: This appendix

covers Windows XP, Vista,

Windows 7, and Windows

Server 2003/2008. For Mac,

it applies to Mac OS X

10.3.x or newer.



www.it-ebooks.info

set up a development environment



Do you have a web server?

You probably already have a web server if you are using a newer PC or

Mac. To find out quickly on either system, open a brower window and

type http://localhost in the address bar. If you get an introductory

page, that means your web browser is alive and well on your local machine.



If you have a Mac or Windows

machine with the Apache web

server installed, you might see

something like this.



If you have a Windows

machine with IIS, you might

see something like this.



Do you have PHP? Which version?

If you have a web server, you can check to see if you have PHP installed very easily, as well as which version you have.

Create a new script named info.php and type this in it:



Save this file to the directory your web server uses. On Windows, it’s typically:

C:\inetpub\wwwroot\ (for IIS)

or:

C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs (for Apache)

On the Mac, it’s usually something like:

/Users/yourname/sites/

If you try to open this file in your browser by typing http://localhost/info.php, you’ll see something like

this if you have PHP installed:



Here’s the version of

PHP you have installed.



you are here 4   463



www.it-ebooks.info

checking your mysql version



Do you have MySQL? Which version?

On Windows, you can tell by right-clicking on the Windows taskbar, selecting Task Manager, and

selecting the Services tab. For more information, you can click the services button on Windows 7.



Here’s where

you’ll see MySQL.



To determine whether you have MySQL on the Mac, open your terminal and type:

cd /user/local/mysql

If the command works, you have MySQL installed. To check the version, type:



The MySQL terminal

is also known as the

MySQL “monitor.”



mysql

File Edit Window Help IHeartPHP



If this

command

succeeds, it

means MySQL

is installed.



Here’s the version

of MySQL you

have installed.



464   appendix ii



$ cd /usr/local/mysql

$ mysql

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.0.51b MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>



www.it-ebooks.info

set up a development environment



Start with the web server

Depending on the version of Windows you have, you can download Microsoft’s Internet

Information Server (IIS), or the open source Apache web server. If you need a server on

the Mac, you should probably go with Apache since it’s already installed.

Here’s a brief overview of installing Apache on Windows:



Head over to http://httpd.apache.org/download.cgi.

If you’re using Windows, we suggest you download the

apache_2.2.19-win32-x86-no_ssl.msi file. This will automatically

install Apache for you after you download and double-click it.



Grab this version

and double-click

it after you’ve

downloaded it.

Next you’ll see the Installation Wizard. Most of

the instructions are straightforward, and you can

accept the default choices.



Choose the domain your

computer is on. If you

don’t have one, you can

enter localhost.



Your best bet is to choose the

typical installation option.



You can usually choose

the default directory for

installation of the software.



you are here 4   465



www.it-ebooks.info

installing php



Apache installation...concluded

You’re nearly finished. Click Install and wait a minute or so for

the installation to complete. That’s it!



Your web server is set to start automatically when you start up your computer.

But you can control it using the Services panel by stopping and starting it in

the Control Panel ➝ Administrative Tools ➝ Services dialogue, where

it will now show up as Apache2.2.

If these instuctions don’t work for you, try again, or type “Installing Apache on

Windows” into your favorite search engine for more help.



PHP installation

Go to http://www.php.net/downloads.php, or http://windows.php.net/download/, if you are using

Windows.

Just as with Apache, if you’re using Windows, we suggest you download the Windows installer

version. If you’re using Apache, download the php-5.2.17-Win32-VC6-x86.msi file. If you’re

using IIS, download the php-5.3.6-Win32-VC9-x86.msi file. This will automatically install PHP

for you after you download and double-click it.



This is the .msi Windows

version download section.



466   appendix ii



Read the description of

which version you should

download.



After you’ve downloaded the file,

double-click it. Click the Run

button to begin the installation.



www.it-ebooks.info

set up a development environment



PHP installation steps

It starts with a basic setup.



Accept the License

Agreement to continue.



Selecting the default installation

folder is usually a good idea, but

it depends on preference. Here,

we choose C:\PHP.



Be careful on this screen. If you’re using Apache, select the right

version. If you’re using IIS, you will probably select the IISAPI

module. Check with your particular software to determine exactly

what you need. Here, we’ve chosen Apache 2.2, and need to give the

path to our Apache install in the next screen.

This next screen is also tricky. You need to scroll down under

Extensions and choose MySQL. This will enable you to use the

built-in PHP MySQL functions that we use throughout this book!



Scroll down below Extensions and click on

MySQL. Click on the “Entire feature” choice.

you are here 4   467



www.it-ebooks.info

installing mysql on windows



PHP installation steps...concluded

That’s it. Click on Install and then Done to close the installer.



If you haven’t done so already, create a new script named info.php and type this in it:



Save this file to the directory your web server uses. On Windows, it’s typically:

C:\inetpub\wwwroot\ (for IIS)

or:

C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs (for Apache)

On the Mac, it’s usually something like:

/Users/yourname/sites/

If you try to open this file in your browser by typing

http://localhost/info.php,

you’ll see something like this if you have PHP installed:



If these instuctions don't

work for you, try again,

or type "Installing PHP

for Apache [or IIS] on

Windows” into your favorite

search engine for more help.



Installing MySQL

Instructions and troubleshooting

You still need MySQL, so let’s work through downloading and installing it. The official name for the

free version of the MySQL RDBMS server these days is MySQL Community Server.

The following is a list of steps for installing MySQL on Windows and Mac OS X. This is not meant to

replace the excellent instructions found on the MySQL website, and we strongly encourage you to go

there and read them! For much more detailed directions, as well as a troubleshooting guide, go here:



Get version 5.5 or newe



http://dev.mysql.com/doc/refman/5.5/en/windows-installation.html



r.



You’ll also like the MySQL query browser, where you can type your queries and see the results inside the

software interface, rather than in a console window.

468   appendix ii



www.it-ebooks.info

set up a development environment



Steps to install MySQL on Windows

1



Go to:

http://dev.mysql.com/downloads/

and click on the MySQL Installer for Windows “Download the Beta”

download button. (Note: It was “Beta” at the time of this writing.)



2



Choose Microsoft Windows from the list.



You may have to scroll

down a little.



you are here 4   469



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

#9. Advanced JavaScript: closures

Tải bản đầy đủ ngay(0 tr)

×