-1

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++) {
...
 }
}
1
  • What is for in? In a method? Outside the class definition? Commented Nov 2, 2012 at 19:53

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

1
function Sample(i, j) {
    this.ivar1 = i;
    this.ivar2 = j;

    this.emptyMethod = function() {};
}

var sample = new Sample(1, 2);

sample.ivar1; // 1

1 Comment

the method should be defined on a prototype, probably. There is no need to have private copy for each instance.
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);

2 Comments

The init function doesn't actually do anything, nor is it necessary.
@Shmiddty yea, I just whipped up an example quick, made edit to add a little more context.
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++) {
// ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.