Tag Archives: Java

Quick Look at JavaScript Objects

JavaScript Objects

As you may know there are different notations of objects in JavaScript, and there are slight differences. However here I’m gonna achieve the same thing with different notations. The first one is the object notation in JavaScript known mostly from JSON. There you’ve to construct a key/value pairs, divided by a colon:

var class1 = {
    a : 0,
    func1 : function() {
        console.log(class1.a);
    },
    func2 : function(a) {
        this.a = a;
    }
}
class1.func2(3);

Here we have two functions – func1 and func2 and one member variable. Func2 is setting up the member variable, it’s something like a setter in some languages like Java and PHP, and the func1 is printing the value of that variable via console.log(). Note that in the body of func1() the variable “a” is accessed with the class name: class1.a. Here the notation can be changed to this.a, which will result in the same thing.

...
console.log(this.a);
...

Notation #2

While in the notation I’ve just used is not possible to have two different instances from the same class, in the next example this is possible. In JavaScript the objects are even the functions, and you can access the function’s private variables with the dot notation.

var class2 = function() {
    this.a = 0;
    this.func1 = function() {
        console.log(this.a);
    }
 
    this.func2 = function(a) {
        this.a = a;
    }
}

To use this object you’ve to use the “new” operator.

var c = new class2();
c.func2(4);

This helps you create more than one instance of the same class.

var c = new class2();
var d = new class2();
c.func2(3);
d.func2(4);

Google Closure Compiler doesn’t work?!

What are these strange java errors?

No, it works but maybe the problem is you current Java version. The Google Closure Compiler requires 1.6 and most commonly you’re runing on 1.5 therefore it produces errors. It was my problem when I tried to run the application. At that moment it produced these lines of errors:

java -jar compiler.jar --help

Exception in thread “main” java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:676)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:317)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:280)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:375)

Just update or switch your current Java version to 1.6 and everything will be fine!

What should I optimize first in my web page?

I start with the optimization process …

It’s a common question! Where do I begin with the optimization process. Should I start with JavaScript or with some simple Apache optimizations? The answer is pretty simple and it’s related to a very, very simple technique.

Look what Firebug is saying

Yeah, look at the Firebug. You can check what’s taking most of the time to load. If it’s some server site script you can try caching it, if it’s a JavaScript file load, than you can try minify/compile it. Another good tool is the Safari developer tool. Than you can graphically understand what part of the sites’ components are slowing down the loading process. Whether the images or the JavaScript, well check that out with the Safari browser.

Finally what I do?

Once you know what’s the thing slowing down the site, start with its optimization! It’s really simple.