Difference between revisions of "JavaScript"
From Suhrid.net Wiki
Jump to navigationJump to search| (4 intermediate revisions by the same user not shown) | |||
| Line 5: | Line 5: | ||
* '''Core''' JS consists of a set of objects e.g. Array, Date and Math and language elements such as operators and control structures. | * '''Core''' JS consists of a set of objects e.g. Array, Date and Math and language elements such as operators and control structures. | ||
* '''Client-side''' JavaScript extends the core language by supplying objects to control a browser and its Document Object Model (DOM). e.g, client-side extensions allow an application to place elements on an HTML form and respond to user events such as mouse clicks, form input, and page navigation. | * '''Client-side''' JavaScript extends the core language by supplying objects to control a browser and its Document Object Model (DOM). e.g, client-side extensions allow an application to place elements on an HTML form and respond to user events such as mouse clicks, form input, and page navigation. | ||
| + | * JS as opposed to say, Java, is loosely typed. | ||
| + | |||
| + | = Values, Variables and Literals = | ||
| + | |||
| + | * JS is dynamically typed. So, the following is possible: | ||
| + | |||
| + | <syntaxhighlight lang="javascript"> | ||
| + | |||
| + | var ans = 42; | ||
| + | |||
| + | ans = "Forty two"; | ||
| + | |||
| + | </syntaxhighlight> | ||
| + | |||
| + | * Variable scope : only two, local and global. No block scope! | ||
| + | * Local variables belong to the defined function. | ||
| + | * Global variables belong to the global object. In web pages, the global object is ''window''. | ||
| + | * An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}). | ||
| + | |||
| + | <syntaxhighlight lang="javascript"> | ||
| + | |||
| + | var cc = "1300cc"; | ||
| + | |||
| + | function carTypes(name) { | ||
| + | if(name == "Vista") { | ||
| + | return "Tata Indica Vista"; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | var car = {myCar : "Vista", getCar : carTypes("Vista"), engine : cc}; | ||
| + | |||
| + | console.log(car.myCar); | ||
| + | console.log(car.getCar); | ||
| + | console.log(car.engine); | ||
| + | |||
| + | </syntaxhighlight> | ||
Latest revision as of 03:34, 6 December 2012
Intro
- JS is a cross-platform object oriented scripting language.
- Is designed for easy embedding in other products and applications, such as web browsers.
- Core JS consists of a set of objects e.g. Array, Date and Math and language elements such as operators and control structures.
- Client-side JavaScript extends the core language by supplying objects to control a browser and its Document Object Model (DOM). e.g, client-side extensions allow an application to place elements on an HTML form and respond to user events such as mouse clicks, form input, and page navigation.
- JS as opposed to say, Java, is loosely typed.
Values, Variables and Literals
- JS is dynamically typed. So, the following is possible:
var ans = 42;
ans = "Forty two";
- Variable scope : only two, local and global. No block scope!
- Local variables belong to the defined function.
- Global variables belong to the global object. In web pages, the global object is window.
- An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}).
var cc = "1300cc";
function carTypes(name) {
if(name == "Vista") {
return "Tata Indica Vista";
}
}
var car = {myCar : "Vista", getCar : carTypes("Vista"), engine : cc};
console.log(car.myCar);
console.log(car.getCar);
console.log(car.engine);