Saturday, July 17, 2010

What’s different in JavaScript?

1. It is case sensitive.

2. JavaScript is a loosely typed language (we don’t need to define the type of the variables).

3. Variables are declared with the keyword “var”.

4. Unlike C, C++, and Java, JavaScript does not have block-level scope. All variables declared in a function, no matter where, are defined throughout the function.

var scope = “global”;

function f(){
   alert(scope); //Displays “undefined”, not “global”*
   var scope = “local”;
   alert(scope); //Displays “local”
{

5. When you declare a global variable you are actually doing is defining a property of the global object.

6. In client-side JS, the window object serves as the global object.

7. Objects map property names to arbitrary property values

8. In JavaScript, a function is a special kind of object that has executable code associated with it. Since functions are objects, they have properties and methods.

9. JavaScript supports function literals. They are just like other function, except that they do not have a name. They can appear within other JavaScript functions:

var square = function(x) { return x*x; }

10. Functions are not only syntax, but also data, which means that they can be assigned to variables, stored in the properties of objects or the elements of arrays, returned from functions, passes as arguments to functions, etc.

Functions can be assigned to an object’s properties. Example:

var dog;

dog.color = function() { … }

In fact, a method is nothing more than a function that is stored in a property of an object, and invoked through that object.

When a function is invoked as a function rather than as a method, the “this” keyword refers to the global object.

11. JS functions can be written so that they work with any number of arguments (p. 129).

12. Functions run in the scope in which they are defined, not the scope from which they are executed (p. 141).

13. Functions are a combination of code to be executed and the scope in which to execute them. This is known as “closure”.

14. JS is a true object-oriented language that uses prototype-based inheritance instead of class-based inheritance.

15. Objects in JS may have ant number of properties, and properties may be dynamically added to an object.

16. Objects are usually created with the keyword “new”.

17. It supports an object literal syntax that allows you to create an object and specify its properties like this (no constructor needed):

var point = {x:3, y:4};

18. Primitive types are manipulated by value, and reference types (objects – including arrays and functions) are manipulated by reference.

19. Strings are immutable.

20. The power of client-side JavaScript is that it has access to the contents of the HTML document.

No comments:

Post a Comment