Important Points - JavaScript and Java are in syntax very different.
- JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, later LiveScript, and finally renamed to JavaScript.
- ECMAScript is the official JavaScript defining standard.
- The DOM interfaces for manipulating web pages are not part of the ECMAScript standard, or of JavaScript itself. Officially, they are defined by a separate standardization effort by the W3C; in practice, browser implementations differ from the standards and from each other.
- Data Types
- boolean
- number
- It is a 64 bit floating point number similar to double in Java.
- There is no integer data type separately in JavaScript like Java.
- string
- String is a sequence of 0 or more UNICODE characters.
- All strings are immutable i.e. they are of fixed length.
- null
- undefined
- object
- Every data besides the above mentioned data types is an object.
- JavaScript defines some built in predefined objects as well as ability to define own custom objects.
- Objects in JavaScript are very similar in their approach to Hashtable when it comes to using them for storing data.
- An object is a referenceable container of name/value pairs.
- The name can be any string.
- When other data types are given as names they are converted into strings. Therefore numbers which are easily convertible to strings can also be used as names. Do not use objects as name since all objects are converted to the same string name.
- JavaScript objects are usually implemented in background as Hashtable.
- Example of Custom Object
- yourobject["Key String"] = value ;
- yourobject.key = value;
- Objects can also be nested inside other objects.
- In the object literal notation, an object description is a set of comma-separated name/value pairs inside curly braces.
- JavaScript's object literals are the basis of the JSON data interchange format.
- The Global Object is the keeper of all of the functions and variables which were not defined inside of other functions and objects. Surprisingly, the Global Object does not have an explicit name in the language.
- In the web browsers, window and self are members of the Global Object which point to the Global Object, thus giving an indirect way of addressing it.
- Array
- Arrays and functions are implemented as objects.
- Arrays in JavaScript are also hashtable objects.
- Arrays in JavaScript can grow in size like Vector in Java.
- The main difference between objects and arrays is the length property.
- The length property is always 1 larger than the largest integer key in the array
- Example of Array Declaration
- var yourArray = [];
- var yourArray = new Array();
- Arrays in JavaScript do not have a specific datatype. They may include numbers, strings, booleans, objects, functions, and arrays inside them.
- When a new item is added to an array and the subscript is an integer that is larger than the current value of length, then the length is changed to the subscript plus one.
- Arrays have a literal notation, similar to that for objects.
- Functions
- When calling a function, it is not required that you pass a fixed number of parameters.
- Excess parameters are ignored.
- Missing parameters are given the value undefined.
- This makes it easy to write functions that deal with optional arguments.
- A function has access to an arguments array.
- JavaScript supports inner and anonymous functions.
- Functions are first class objects in JavaScript. That means that they can be stored in objects and passed as arguments to functions.
- There are three notations for defining functions:
- function statement
- function operator
- function constructor
- Recursion is allowed on JavaScript functions.
- When a function is a member of an object, it is called a method.
- Functions which are used to initialize objects are called constructors.
- Functions can be defined inside of other functions. The inner function has access to the vars and parameters of the outer function.
- JavaScript does not have a void type, so every function must return a value. The default value is undefined, except for constructors, where the default return value is this.
- JavaScript includes an eval function that can execute statements provided as strings at run-time.
- Prototype
- JavaScript uses prototypes instead of classes for inheritance. It is possible to simulate many class-based features with prototypes in JavaScript.
- Objects contain a hidden link property.
- When items are accessed from an object by the dot notation or the subscript notation, if the item is not found in the object then the link object is examined. If it is not found in the link object, and if the link object itself has a link object, then that link object is examined. If the chain of link objects is exhausted, then undefined is returned.
- This use of prototype link chains provides a sort of inheritance.
- Variables
- Named variables are defined with the var statement.
- When used inside of a function, var defines variables with function-scope.
- Any variables used in a function which are not explicitly defined as var are assumed to belong to an outer scope, possibly to the Global Object.
- Vars which are not explicitly initialized are given the value undefined.
- A new set of vars is made every time the function is called. This allows functions to be recursive.
- 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.
- Statements
- if, switch, for, while, do, break, continue, return, try, throw work like C type of languages.
- In if statements, while statements, do statements, and logical operators, JavaScript treats false, null, undefined, "" (the empty string), and the number 0 as false. All other values are treated as true.
- The case labels in a switch statement can be expressions. They don't have to be constants. They can be strings.
- There are two forms of the for statement. The first is the common (init; test; inc) form. The second is an object iterator.
- Operators
- JavaScript has a fairly large set of operators. Most of them work the same way as in other C-like languages. There are a few differences to watch out for.
- Reserved Words
- abstract
- boolean break byte
- case catch char class const continue
- debugger default delete do double
- else enum export extends
- false final finally float for function
- goto
- if implements import in instanceof int interface
- long
- native new null
- package private protected public
- return
- short static super switch synchronized
- this throw throws transient true try typeof
- var volatile void
- while with
JSON (JavaScript Object Notation) - Very convenient to codify data
- Very simple to understand
- Very Powerful in terms of data modelling
- Native JSON Support
- Firefox 3.5 is having it
- Google Chrome 4.1 is having it but gives error in certain cases where Firefox works
External Links JSON The text on this page is under Creative Commons Attribution License. |