Thursday, February 21, 2008

Q. What is Javascript? Discuss its features.
JavaScript is a scripting language most often used for client-side web development. It was the originating dialect of the ECMAScript standard. As such, it is a dynamic, weakly typed, prototype-based language with first-class functions.

JavaScript was influenced by many languages and was designed to have a similar look to Java, but be easier for non-programmers to work with. The language is best known for its use in websites (as client-side JavaScript), but is also used to enable scripting access to objects embedded in other applications (for example Microsoft Gadgets in Windows Vista Sidebar).

Despite the name, JavaScript is essentially unrelated to the Java programming language, though both have a common debt to C syntax, and JavaScript copies many Java names and naming conventions. The language was renamed from LiveScript in a co-marketing deal between Netscape and Sun in exchange for Netscape bundling Sun's Java runtime with their browser, which was dominant at the time. The key design principles within JavaScript are inherited from the Self programming language.

"JavaScript" is a trademark of Sun Microsystems. It was used under license for technology invented and implemented by Netscape Communications and current entities such as the Mozilla Foundation.



Features

Structured programming
JavaScript supports all the structured programming syntax in C, e.g. if statement, while loops, switch statement, etc. One exception is scoping: JavaScript supports function-level scoping, but not block-level scoping.

Dynamic programming
dynamic typing
As in most scripting languages, types are associated with values, not variables. For example, a variable x could be bound to a number, then later rebound to a string. JavaScript supports various ways to test the type of an object, including duck typing.
objects as associative arrays
JavaScript is heavily object-based. Objects are associative arrays, such that object property names are associative array keys. obj.x = 10 and obj["x"] = 10 are equivalent, the dot notation being merely syntactic sugar. Properties and their values can be added, changed, or deleted at run-time.
interpreted
Conforming JavaScript engines must be able to interpret (as opposed to compile) source code. This allows JavaScript to include an eval function.

Function-level programming
first-class functions
Functions are first-class; they are objects themselves. As such, they have properties and can be passed around and interacted with like any other object.
inner functions and closures
Inner functions (functions defined within other functions) are created each time the outer function is invoked, and variables of the outer functions for that invocation continue to exist as long as the inner functions still exist, even if that invocation is finished (e.g. if the inner function was returned) — this is the mechanism behind closures within JavaScript.

Prototype-based
prototypes
JavaScript uses prototypes instead of classes for defining object properties, including methods, and inheritance. It is possible to simulate many class-based features with prototypes in JavaScript.
functions as object constructors
Functions double as object constructors along with their typical role. Prefixing a function call with new creates a new object and calls that function with its local this keyword bound to that object. The function's prototype property determines the new object's prototype.
functions as methods
Unlike many object-oriented languages, there is no distinction between a function definition and a method definition. Rather, the distinction occurs during function calling; a function can be called as a method. When a function is invoked as a method of an object, the function's local this keyword is bound to that object.

Others
run-time environment
JavaScript typically relies on a run-time environment (e.g. in a web browser) to provide objects and methods by which scripts can interact with "the outside world". (This is not a language feature per se, but it is common in most JavaScript implementations.)
variadic functions
An indefinite number of parameters can be passed to a function. The function can both access them through formal parameters and the local arguments object.
regular expressions
JavaScript also supports regular expressions in a manner similar to Perl, which provide a concise and powerful syntax for text manipulation that is more sophisticated than the built-in string functions.




HTTP
Hypertext Transfer Protocol (HTTP) is a communications protocol for the transfer of information on intranets and the World Wide Web. Its original purpose was to provide a way to publish and retrieve hypertext pages over the Internet.

HTTP development was coordinated by the World Wide Web Consortium (W3C) and the Internet Engineering Task Force (IETF), culminating in the publication of a series of Request for Comments (RFCs), most notably RFC 2616 (June 1999), which defines HTTP/1.1, the version of HTTP in common use.

HTTP is a request/response standard between a client and a server. A client is the end-user, the server is the web site. The client making an HTTP request - using a web browser, spider, or other end-user tool - is referred to as the user agent. The responding server - which stores or creates resources such as HTML files and images - is called the origin server. In between the user agent and origin server may be several intermediaries, such as proxies, gateways, and tunnels. HTTP is not constrained to using TCP/IP and its supporting layers, although this is its most popular application on the Internet. Indeed HTTP can be "implemented on top of any other protocol on the Internet, or on other networks. HTTP only presumes a reliable transport; any protocol that provides such guarantees can be used."

Typically, an HTTP client initiates a request. It establishes a Transmission Control Protocol (TCP) connection to a particular port on a host (port 80 by default; see List of TCP and UDP port numbers). An HTTP server listening on that port waits for the client to send a request message. Upon receiving the request, the server sends back a status line, such as "HTTP/1.1 200 OK", and a message of its own, the body of which is perhaps the requested file, an error message, or some other information.

Resources to be accessed by HTTP are identified using Uniform Resource Identifiers (URIs) (or, more specifically, Uniform Resource Locators (URLs)) using the http: or https URI schemes.





Q. Control Structures in Javascript.

A control structure, as the term implies, refers to the flow of execution of the program. There are two possible control structures: linear and non-linear. The linear execution (also called sequential execution) refers to the execution of the statements in the order they are listed. In comparison, the non-linear execution refers to the execution of statements regardless of the order they are listed.

There are three ways to change the control structures of a program by using:

1. conditional statements (also called conditional logic)
2. looping control
3. branch logic


1. Conditional Statements: A conditional statement uses conditional logic to determine what programming statements to execute. By using some conditional logic (a process of checking conditions), we determine if a certain condition is true or false. If the condition is true, we execute some part of the program. Otherwise, if the condition is false, we use some other part of the program.

To create a condition in JavaScript, we need to write a conditional statement with a comparison operator. (JavaScript also supports logical operators to create complex conditions.) A comparison operator compares the value of one element with that of another. An expression that uses a comparison operator is referred to as a Boolean expression. A Boolean expression evaluates to either true or false.

For eg: if (5 > 6) {document.write ("Condition is true : 5 is less than 6");}else {document.write ("Condition is false : 5 is not greater than 6");}

2. Looping Control: Looping or iteration control is used to execute some set of instructions repeatedly. Without loops, we would have to write the instructions as many times we want them to execute.

There are two types of loops:
1. loops that repeat a set number of times before quitting. This type of loop is created with a for loop. You can use a for loop when you know exactly how many times you want to execute some code.
2. loops that repeat until a certain condition is satisfied. This type of loop is created with a while loop. The while is useful for those situation in which it is known how many times a specific code needs to be run.

(a) For Loop: With a for loop, we can run a group of statements a set number of times through the use of a counter, a variable that tracks the number of times the group of statements has been run. To create a for loop, we set an initial value for the counter variable and each time our command block is executed, we change the value of the counter. When the value of the counter variable reaches our stopping value, the loop will end. A general syntax for creating a JavaScript loop is:

for (startingValue, condition, iterationValue) { JavaScript statements that you want to execute repeatedly}

where
startingValue = the initial value of the counter variable
condition = a Boolean expression that must be true for the loop to continue and must become false for the loop to stop
iterationValue = a value to change the value of the counter variable

eg. script language="javascript">for (counter=1; counter <=5; counter++) {document.write(counter + "
");}script


(b) While Loop: The while loop is different from a for loop in the respect that it is known ahead of time how many times a while loop will execute. Suppose you ask the user for some input, the user may not enter the value you seek in the first try. So using a while loop, we would keep requesting for the right input. But if you use a for loop, you would be limited to asking only once, twice, or however many times you use. The point is with a for loop, you initially specify how many times the for loop is to be executed, but with a while loop that is not necessarily true.

The general syntax for creating a while loop is:
while (condition) {JavaScript statements that you want to execute repeatedly}
wherecondition = a Boolean expression that can be true or false. While the Boolean expression is true, the JavaScript code inside the while loop is executed.

eg. script language="javascript">var i = 1;while (i <= 5) {document.write (i + " ");i++;}script

3. Branch Logic:If you have to write a large program, you may consider using functions. Functions allows you to easily reuse your code. A function is a series of commands that either calculates a value or performs an action. A function consists of function name, which identifies it, parameters, the values passed to the function, and the set of commands that run when the function is called. To use a function, you call the function. When you call the function, you pass (send) the values used by the function. Note that not all functions require you to pass values; thus parameters are optional. Functions can also return a value, the result of the calculation or the output of the function sent back from the function.
The general syntax of a JavaScript function is:
function functionName (parameters) {JavaScript commands}
where
functionName = name of the function
parameters = values sent to the functions
JavaScript commands = commands that run when the function is called or used
A function starts with the keyword function. All the commands that belong to a function must be placed inside the curly braces { }. The set of commands placed inside the curly braces is referred to as a commands block. The commands block combined with the function name is referred to as a function definition. In other words, a function definition tells you what the function is and what it does.
Before you start creating your own JavaScript functions, keep in mind that JavaScript function name must begin with a letter or underscore (_). The function name cannot contain any spaces. Also, like variables, function names are case-sensitive. Thus calculateSalary and CalculateSALARY are considered different function names. For the function parameters, there is no limit in the number of function parameters a function contains. The function parameters must be placed inside the parentheses, following the function name, and each parameter must be separated by a comma.

Q. Document Object Model in Javascript

To create a interactive web page it is necessary that the browser continues to recognize individual HTML objects even after they are rendered in the browser window. This allows the browser to access the properties of these objects using built-in methods of the object. Once the properties of the object are accessible then the functionality of the object can be controlled at will.

Javascript enabled browsers are capable of recognizing individual objects in an HTML page, after the page has been rendered in the browser, because the javascript enabled browser recognizes and uses the Document Object Model.

The topmost object in the DOM is the 'Navigator', i.e., the browser itself. The next level in the DOM the browser's 'window'. The next level in the DOM is the 'Document' displayed in the browser's window.

The DOM heirarchy continues downwards to encompass individual elements on a 'FORM', such as Text boxes, Labels, Radio Buttons, Check Boxes, Push Buttons and so on, which belong to the form.

Javascript's object heirarchy is mapped to the DOM which in turn is mapped to the web page elements in the browser window. Hence, when a wb page is rendered in a Javascript enabled browser window, Javascript is able to uniquely identifying each element in the web page, because major elements of a web page are bound to the DOM.

Q. Javascript Objects.

1. String

String Object Properties:

Constructor - A reference to the function that created the object

length - Returns the number of characters in a string

prototype - Allows you to add properties and methods to the object

String Object Methods:

anchor, big, blink, bold, charAt, fixed, fontColor, fontSize, indexOf, italics, lastIndexOf, link, small, split, strike, sub, substring, sup, toLowerCase, toUpperCase

Event handlers: Window

2. Date

properties: none

methods: getDate, getDay, getHours, getMinutes, getMonth, getSeconds, getTime, getTimeZoneoffset, getYear, parse, prototype, setDate, setHours, setMinutes, setMonth,, setSeconds, setTime, setYear, toGMTString, toLocaleString, UTC

event handlers: none

No comments: