0

Can this be done in Javascript? I am converting a java wiki page to javascript. I'm pretty sure var should be used instead of int right?

class Sample extends Object {

 int ivar1;
 int ivar2; 

Sample(int i, int j) {
 ivar1 = i;
 ivar2 = j;

}

int emptyMethod() {}
...

for (int i = 0; i < maxLoops; i++) {
...
 }
}
Anthony
  • 2,330
  • 8
  • 44
  • 64

4 Answers4

2

Try taking a look into either prototypes or closures.

From MDN (prototypes):

All objects in JavaScript are descended from Object; all objects inherit methods and properties from Object.prototype Object.prototype, although they may be overridden (except an Object with a null prototype, i.e. Object.create(null)). For example, other constructors' prototypes override the constructor property and provide their own toString methods. Changes to the Object prototype object are propagated to all objects unless the properties and methods subject to those changes are overridden further along the prototype chain.

Many agree that changing the Object type directly can cause issues, especially when linking in other libraries. Becasue of this, it's usually best to use closures.

From MDN (closures):

Languages such as Java provide the ability to declare methods private, meaning that they can only be called by other methods in the same class.

JavaScript does not provide a native way of doing this, but it is possible to emulate private methods using closures. Private methods aren't just useful for restricting access to code: they also provide a powerful way of managing your global namespace, keeping non-essential methods from cluttering up the public interface to your code.

Community
  • 1
  • 1
Chase
  • 29,019
  • 1
  • 49
  • 48
1
function Sample(i, j) {
    this.ivar1 = i;
    this.ivar2 = j;

    this.emptyMethod = function() {};
}

var sample = new Sample(1, 2);

sample.ivar1; // 1
David G
  • 94,763
  • 41
  • 167
  • 253
  • the method should be defined on a prototype, probably. There is no need to have private copy for each instance. –  Nov 02 '12 at 19:56
1

There are different ways to loosly mimmick this behavoir. There are several libraries avaiable to help you build more object oriented javascript like:

  • Prototype.js
  • Backbone.js
  • Angular.js

Using Prototypes in a loose example (not the prototype framework):

function Sample() {};
var i = new Sample();
var x = new Sample();    

Sample.prototype.init = function() {
    this.ivarA = "constant 1";
    this.ivarB = "constant 2";
}
Sample.prototype.set = function(i, j) {
    this.ivar1 = i;
    this.ivar1 = j;
}

//example of override
m.set = function(i, j) {
    this.ivar1 = i + j;
    this.ivar2 = i + i + j;    
}

i.init();
i.set("hey", "whats up?");
x.set(10, 5);
Dan Kanze
  • 18,485
  • 28
  • 81
  • 134
1
// class Sample ...
// (including constructor)
function Sample (i, j) {

  this.ivar1 = i;
  this.ivar2 = j;

}

// ... extends Object
Sample.prototype = Object.create(Object.prototype);
// (the first Object is part of call, the second Object is the one from 'extend')

Sample.prototype.emptyMethod = function () {};
...

// Don't know where to put, probably error in original question:
// for (var i = 0; i < maxLoops; i++) {
// ...