JavaScript

JS Strict Data Types - Integer

Tagged:  

Back to Main Documentation Page
The JS Strict Data Types Integer method allows you to define a variable as an Integer data type and maintain the value as an integer.

To identify a variable as an Integer you would write the below code (where OptionalIntegerValue is some Integer value, if no value is inserted then 0 is returned).

MyVar1 = new Int(OptionalIntegerValue);

To set the value of the Integer to a new value you would use the below code (where IntegerValue is some Integer value).

MyVar1.Set(IntegerValue);

To return the data type of the Integer you would use the DataType property.

alert(MyVar1.DataType);

To get the value of an integer you would use the value property (the below would display the value).

alert(MyVar1.value);

To add an Integer value to the Integer you would use the add method as seen below (where IntegerValue is some Integer value).

MyVar1.add(IntegerValue);

To subtract an Integer value from the Integer you would use the sub method as seen below (where IntegerValue is some Integer value).

MyVar1.sub(IntegerValue);

To divide the Integer by an Integer value you would use the div method as seen below (where IntegerValue is some Integer value).

MyVar1.div(IntegerValue);

To multiply the Integer by an Integer value you would use the mult method as seen below (where IntegerValue is some Integer value).

MyVar1.mult(IntegerValue);

To compare if an Integer is equal to the Integer value you would use the CompareEq method as seen below (below is how you would use the CompareEq method in an if statement, where IntegerValue is some Integer value).

if(MyVar1.CompareEq(IntegerValue)){
    alert("Statement is Equal");
{

To compare if an Integer is not equal to the Integer value you would use the CompareNotEq method as seen below (below is how you would use the CompareNotEq method in an if statement, where IntegerValue is some Integer value).

if(MyVar1.CompareNotEq(IntegerValue)){
    alert("Statement is not Equal");
{

To compare if the Integer value is greater than an Integer value you would use the CompareGrt method as seen below (below is how you would use the CompareGrt method in an if statement, where IntegerValue is some Integer value).

if(MyVar1.CompareGrt(IntegerValue)){
    alert("Statement Greater Than");
{

To compare if the Integer value is greater than or equal to an Integer value you would use the CompareGrtEq method as seen below (below is how you would use the CompareGrtEq method in an if statement, where IntegerValue is some Integer value).

if(MyVar1.CompareGrtEq(IntegerValue)){
    alert("Statement Greater Than or Equal");
{

To compare if the Integer value is less than an Integer value you would use the CompareLess method as seen below (below is how you would use the CompareLess method in an if statement, where IntegerValue is some Integer value).

if(MyVar1.CompareLess(IntegerValue)){
    alert("Statement Less Than");
{

To compare if the Integer value is less than or equal to an Integer value you would use the CompareLessEq method as seen below (below is how you would use the CompareLessEq method in an if statement, where IntegerValue is some Integer value).

if(MyVar1.CompareLessEq(IntegerValue)){
    alert("Statement Less Than or Equal");
{

Below is an example of how you would use a function to maintain an Integer value.

function TestFunction(MyVar1){
    //Set the Data Type of each Variable
    MyVar1 = new Int(MyVar1);
    alert("MyVar1 = " + MyVar.value);
}

Below is an example of how you would accomplish a for loop using the Integer method.

for(var a = new Int(0); a.value<20; a.add(1)){
    alert("a = " + a.value);
}

Microsoft and Nokia adopt jQuery

Announced on the jQuery blog, and by John Resig here at the Ajax Experience, jQuery is being adopted by Microsoft and Nokia as part of their official development platform. Microsoft will distribute the framework with Visual Studio and will build UI widgets to run on top of jQuery. Nokia is looking to use jQuery to develop applications for their WebKit-based Web Run-Time. To make this news even sweeter, Microsoft and Nokia aren’t looking to make any modifications to jQuery (both in the form of code or licensing) - they plan to use it as-is. This is some very exciting news and we'd like to congratulate the jQuery team on this milestone!

Read the full post.

Bluff - JavaScript Graphs

If you want to display data in a graph dynamically in a web page, you have a few options. You can create your own library to create the Graph or use an existing one. If you decide against using the Google Chart API you may want to look into Bluff. Bluff is a JavaScript library that uses canvas (Google’s ExCanvas is used to allow Internet Explorer support) to create a graph on a web page.

Below is an example of a graph and the code that is used to create it.

        <canvas id="example"></canvas>
        
        <script type="text/javascript">
          var g = new Bluff.Line('example', 400);
          g.theme_37signals();
          g.title = 'My Graph';
        
          g.data('Apples', [1, 2, 3, 4, 4, 3]);
          g.data('Oranges', [4, 8, 7, 9, 8, 9]);
          g.data('Watermelon', [2, 3, 1, 5, 6, 8]);
          g.data('Peaches', [9, 9, 10, 8, 7, 9]);
        
          g.labels = {0: '2003', 2: '2004', 4: '2005'};
        
          g.draw();
        </script>

You can download Bluff here.

You can read more about Bluff here.

b9j.uri : A JavaScript URI Object

Robert Krimen has created a JavaScript URI Object capable of parsing, manipulating, and generating URIs. It splits up a URI into three objects: the URI, the path, and the query. Changes to the path or query objects are reflected in their respective URI. Armed with a wide array of methods, the b9j.uri object is well equipped to handle even the toughest of URIs.

Here's a overview of b9j.uri.URI from Robert:

  • Parses using Steven Levithan's parseUri 1.2
  • Easy path traversal (ascend to parent, descend to a child)
  • Set or add to the URI query (with proper encoding), get/interrogate the query (with proper decoding)
  • Add arbitrary data (unencoded) to the end of the query string, for extra flexibility
  • Manipulate/interrogate the scheme, user information (username & password), host, port, fragment, and more in a piecewise fashion
  • Add, change or strip the extension (html, js, css, ...) from a given URI

The code is available at http://appengine.bravo9.com/b9j/b9j.uri.js (19kb compressed / 5kb gzipped)

Code Snippet: Detect Google Chrome w/JavaScript

With Chrome entering the browser market, you may find a need in the not too distant future to code some exceptions for it. In case that day comes, here's a quick snippet of JavaScript for detecting if your users are Chromed out:

You can detect Chrome using the following:
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

Chrome's full user agent:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.27 Safari/525.13

Google Chrome

Yesterday, the official Google blog broke the news that, yes, Google is indeed developing its own browser called Google Chrome. Much of the motivation and thinking behind this move is described interesting enough in an online comic strip.

Early news:

  • It is based on WebKit, the open source engine behind Safari
  • The browser runs with a JavaScript virtual machine codenamed "V8" that was developed from scratch by a team in Denmark
  • Each tab in the browser will represent a separate process, and will have its own controls and address bar
  • Google's Gears project will be bundled with the browser
  • The browser will have multiple tabs and an auto-complete address bar (like Firefox)
  • There will be a "privacy mode" where no traces of browsing history are left on the computer
  • Web applications can be launched in their own window (sans address bar, etc), an "RIA" feature
  • Built in protection from phishing and malware

According to the blog entry the beta version of Chrome should be available sometime today at this page, but as of this writing the URL just returns a "Page Not Found" (404).

Update: Chrome is now available at the above link.

GWT 1.5 Released

The Google Web Toolkit (GWT) project has released version 1.5 of its Java-based web development library yesterday. The new release sports a host of new features such as:

  • Support for Java 5 language features (generics, enums, enhanced for loops, and static imports)
  • Improved performance of compiled JavaScript code
  • It is now possible to sublcass the JavaScriptObject class, creating "class overlays" that can be mapped to arbitrary JavaScript classes
  • Expanded JRE emulation library
  • A new high-performance DOM API
  • Default visual themes: Standard, Chrome, and Dark
  • Support for right-to-left languages

For a full list of the new features, see here.

The new version can be downloaded here.

Firebug 1.2 Released

Tagged:  

Firebug, the latest version of the indispensable debugging utility for Firefox, has released version 1.2 of the plugin for Firefox 3.0.

What's New?

  • Selective Panel Enablement: You can now choose to enable any combination of Console (logging), Script (JavaScript debugging) and Net (Network monitoring).
  • Automatic activation of HTML, CSS, and DOM views when Firebug UI is visible/activated (since they require minimal overhead).
  • Ability to suspend/resume Firebug.
  • A new tooltip telling you which tab/page in Firefox has enabled Firebug.

John Resig, creator of jQuery and now lead on the Firebug team, gives an excellent overview on his blog. Resig points out the currently rather global nature of enabling certain Firebug features, the overhead of enabling them (especially Script, which slows down all JavaScript running in Firefox about 25%), and states that all of these issues are targets for the 1.3 release.

Use the Add-ons panel in Firefox to update your plugin or get it now.

The release notes are here.

Faster JavaScript in Firefox 3.1

Tagged:  

Mozilla plans to give some relief to web developers struggling to create a "desktop-like" experience for web applications: relief in the form of huge increases in speed. On Friday, the father of JavaScript, Brendan Eich, announced the launch of a new JIT (Just In Time) JavaScript compiler called TraceMonkey that will be included in Firefox 3.1.

Depending on the benchmark, TraceMonkey currently improves JavaScript performance in Firefox 3.1 by about 2 to 37 times over Firefox 3.0. The average performance improvement is predicted to be about 4.6 times faster.

TraceMonkey is an evolution of Mozilla's current JavaScript engine, SpiderMonkey, that also draws from the Adobe-contributed Tamarin Tracing project. It is based on the concept of tracing (or trace-based compilation), which takes the approach of monitoring bytecode interpretation, following frequently-executed backwards branches to a "loop start point", analyzing the linear sequence of instructions in what is called a "trace" (using data structures called Trace Trees), and natively compiling these code paths. This has the advantage over more traditional JIT compilers--that keep track of which methods get called most frequently, and do whole-method analysis and compilation--of having to analyze and natively compile only the performance-critical parts of the code. It also has the advantage of being more mobile-friendly as this translates to a much lighter memory footprint. Andreas Gal, the principal architect of the TraceMonkey project, discusses the technique in his blog.

According to Gal, Firefox now has the "fastest JavaScript engine in the world". So what can you do when your browser's JavaScript is that fast? How about online photo editing...

ECMAScript Harmony

Tagged:  

Big news for the future of the Web: JavaScript 2 (ECMAScript 4) is dead, though pieces of it will live on in the new ECMAScript 3.1-based (informally dubbed "Harmony") specification. The members of the ECMA Technical Committee 39 (Adobe, Mozilla, Opera, and Google in favor of ECMAScript 4 and Microsoft, Yahoo in favor of the less ambitious ECMAScript 3.1), which had been at odds over the future direction of the JavaScript language for some time, finally agreed upon the new direction. Brendan Eich, the original creator of JavaScript, broke the news on Wednesday (8/13).

The details of what the new unified specification will look like are far from finalized at this point, but clearly it will be a very scaled back version from the ambitious ECMAScript 4 proposal. Some early results:

  • Packages, namespaces, and early binding from ES 4 are off the table for good.
  • ES 4 classes are being "rephrased": they will now be syntactic sugar for lambda-coding and Object.freeze() (which was proposed in ES 3.1)
  • JavaScript getters and setters are being fast-tracked as part of the new specification.
  • ES 4 let expressions seem to have some general agreement among committee members.

The announcement has sent ripples across the Web. Adobe, which had built ActionScript 3 to closely match anticipated ECMAScript 4 features, has maintained that they will not change AS 3 at all in response to the new direction. Some have speculated that Microsoft's motivation in bucking the ECMAScript 4 standard--and certainly the main factor in why it was abandoned--was political.

Whatever the reasons for abandoning ECMAScript 4, it is certainly clear that JavaScript will now evolve at a much slower pace than some had hoped or anticipated.

Syndicate content