Thursday 12 July 2012

What is JavaScript ?


What is JavaScript ?

JavaScript is, technically speaking, an object-oriented, weakly typed, scriptinglanguage. One could toss more jargon into this definition, but those are the mostcritical aspects of the language. Let’s look at them in detail. First, JavaScript is an object-oriented programming language, as opposed to aprocedural one. This distinction has several implications. First and most importantamong these is that almost all of the variables you’ll work with are, in fact,objects. An object is a special variable type that can have its own subvariables,called properties, and functions, called methods. Together, an object’s propertiesand methods are called its members. For example, here is a string in JavaScript, a string being any number of quotedcharacters:

var name = ‘Larry Ullman’;

That string variable, name, is actually an object of type String. Because it’s aJavaScript String object, name automatically has a property called length, whichreflects the number of characters in the string. For this particular string, length hasa value of 12, which includes the space. Similarly, name automatically has severaldefined methods, like substring() and toUpperCase(). (With an object’s members,the parentheses distinguish properties from methods. )With object-oriented programming, you’ll use object notation extensively to referto an object’s members: someObject. someProperty or someObject. someMethod(). This means that, using the name example, name. length has a value of 12, and tocapitalize the string, you could code

name = name. toUpperCase(); // Now ‘LARRY ULLMAN’

Conversely, in procedural PHP code, you would write

$name = ‘Larry Ullman’; $name = strtoupper($name); // Now ‘LARRY ULLMAN’

And

$length = strlen($name); // 12

As you can see, to apply a function to a variable in procedural code, the variableis passed to the function as an argument. In object-oriented code, the variable’sown function (i. e. , its method) is called by the object itself.

The object (or dot) notation can also be chained, allowing you to access nested properties and methods:

someObject. someProperty. someMethod()

The fact that JavaScript is an object-oriented language is quite significant andhas many ramifications as to how the language can be used. In fact, as you’ll eventuallysee, even functions and arrays in JavaScript are objects! JavaScript is a differentkind of OOP language, though, in that you don’t define classes and then createobjects as instances of those classes, as you do in most object-oriented languages. As you’ll learn in time, this is because JavaScript is protoype-based, not class-based. This somewhat uncommon type of object-oriented language changes how youperform OOP in JavaScript, especially in more advanced-level programming.

NOTE: it’s conventional in ooP to use camel-case for variable and function names: someObject and someMethod(), not some_object and some_method().

The second part of the JavaScript definition says that JavaScript is a weaklytyped language, meaning that variables and data can be easily converted fromone type to another. For example, in JavaScript, you can create a number and thenconvert it to a string:The second part of the JavaScript definition says that JavaScript is a weaklytyped language, meaning that variables and data can be easily converted fromone type to another. For example, in JavaScript, you can create a number and thenconvert it to a string:

var cost = 2; cost += ‘ dollars’; // cost is now a string: “2 dollars”

In a strongly typed language, the creation of a new variable, such as cost, wouldalso require indicating its strict type. Here is how the variable declaration andassignment would be done in ActionScript, a language otherwise very similar toJavaScript:In a strongly typed language, the creation of a new variable, such as cost, wouldalso require indicating its strict type. Here is how the variable declaration andassignment would be done in ActionScript, a language otherwise very similar toJavaScript:

var cost:int = 2; // cost must be an integer!

Moreover, in a strongly typed language, attempts to convert a number to astring (as in the JavaScript code) would generate an error. Some programmers appreciate the flexibility that weakly typed languagesoffer; other programmers consider weak typing to allow for sloppy coding. To befair, bugs can occur because of implicit type conversion. (JavaScript is also called dynamically typed, because conversions can happen automatically, as in the abovecode. ) But if you’re aware of type conversions as you program, the potential forbugs will be mitigated and you can take full advantage of the language’s flexibility. Third, to say that JavaScript is a scripting language means that JavaScript codeis run through a program that actually executes the code. By comparison, theinstructions dictated by a language such as C must first be compiled and then thecompiled application itself is executed. In this web-site, almost all of the JavaScript willbe executed within a Web browser, where the JavaScript “executable” is the Webbrowser’s JavaScript engine (and different browsers use different JavaScript engines).

No comments:

Post a Comment