Skip to main content

JavaScript Classes

ECMAScript 6 comes with the ability to use classes. JavaScript being a prototype based, classless object oriented programming language, doesn't have the notions of classes. Then how can they assimilate them? First, let's have a look at how one would traditionally use object creation and inheritance in JavaScript:

    //function imitating constructor
    function CustomObject(value){
        //instance property; each occurrence of CustomObject will have it's own with a unique value 
        this.property = value;
    }

    //prototype method; each occurrence of CustomObject will have access to this method 
    CustomObject.prototype.method = function(){
        return this.property;
    }

    //create new object
    var obj = new CustomObject("my value");

    //function imitating subclass constructor
    function SubClassObject(value){
        //similar to calling super in class based languages
        CustomObject.call(this, value);
    }

    //set the prototype of the sub object
    SubClassObject.prototype = new CustomObject(null);

    //now we can create an instance of the sub object
    var subObj = new SubClassObject("some value");

    //still can access "super class" methods and properties
    subObj.method(); 

Now, with ECMAScript 6 one would write:

 class CustomObject{

    constructor(value){
        this.property = value;
    }

    method(){
        return this.property;
    }

  }

class SubClassObject extends CustomObject{
    constructor(value){
        super(value);
    }
  }

But doesn't this make it a class based language? No, JavaScript still remains prototype based, the new class structure is simply referred to as "syntatic sugar". This means, even though it looks like a class based language, "behind the scenes" it's working as a prototype based language. For some people, including myself, it just is much easier to follow a class based scheme. Hopefully, this makes the learning curve from languages such as Java to JavaScript much easier. Though, to be successful at JavaScript, I believe, it's still crucial to know how the "deep down" prototype based paradigm works. 

Comments

Popular posts from this blog

Face detection and live filters

Live video filters are becoming a popular trend fueled by Facebook (through their purchase of Msqrd) and Snapchat incorporating the features into their apps. These filters apply images or animations to your face using face tracking software. This technology has been around for awhile but is becoming increasingly more common due to the powerful CPU's that our mobile phones now have. Google provides an API that provides face tracking abilities through the Google Play Services library called Mobile Vision. I'm going to use their API to build a basic live filter app. The end result will look something like this:


    The bounding box wraps around the detected face and the sunglasses are the filter I chose (which is just a PNG image) which are drawn over the eyes. You could use any PNG image (with alpha for the background) you want, you will just have to adjust the layout according to where the image should be displayed. As you move your head, the box and sunglasses are redrawn…

Setting Up Connection Pooling With Elastic Beanstalk

Amazon's Elastic Beanstalk is a service which automatically scales your application when needed. It uses Amazon's Elastic Compute Cloud (EC2) instances as deployable containers which when your app requires more resources more containers will be deployed. This removes the need to manually configure your EC2 instance whenever you need more connections or resources and attempts to add simplicity to the maintenance aspect of your application. So, when you get more users of your app, your app will scale accordingly.

    Unfortunately, along with the ability to scale automatically, comes less control and configuration. Things you would normally have the ability to configure to your liking, such as your server, you no longer can. Amazon attempts to address this issue with configuration files. You can provide configuration files which can set up your server. These files are either written in JSON or the horrible format YAML. Though these files provide some level of control, you ca…

Android Guitar Tuner

Recently I created a guitar tuner application for Android that is written with pure Java (no C++ or NDK usage). The design was inspired by the Google Chrome team's guitar tuner web app using the WebAudio API. I wanted to code a version written natively for Android that didn't have to rely on a WebView, the WebAudio APIs, or server-side logic. Also, I wanted this application to be available to as many versions of Android as possible (whereas the WebAudio API may only be supported in more recent versions of WebView available only on newer flavors of Android). So, I coded it from scratch. I used a portion of the open source TarsosDSP project (their YIN algorithm) to help with the pitch detection.

    The application is available in the Google Play Store for Android: https://play.google.com/store/apps/details?id=com.chrynan.guitartuner. The project is completely open source and the code can be found on the GitHub repository: https://github.com/chRyNaN/Android-Guitar-Tuner. Fi…