Variable access using JavaScript OO

Based on Java coding:

public Class Dog {
    int a = 1;;
    int b = this.a;
    public int c() {
        return a+b;
    }
}

Corresponding with JavaScript, we have:

function Dog() {
    d = new Object();
    d.a = 1;
    d.b = d.a;
    d.c = function() {
        return this.a+this.b;
    }
    return d;
}

Remember in JavaScript, if variable b want to get variable a, should use d.a but not this.a, however inside method, want to get variable a, should use this.a instead of d.a.

This is a kind of JavaScript Object-oriented reliazation method.

Author: fyhao

Jebsen & Jessen Comms Singapore INTI University College Bsc (Hon) of Computer Science, Coventry University

2 thoughts on “Variable access using JavaScript OO”

  1. as the javascript, is it you called like this?
    var dog = new Dog();
    alert(dog.c());
    ?
    However, I am tested in Google Chrome, haven’t tested in other browsers.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.