Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a class in classic jscript?

how to create a class in classic jscript? (not jscript.net)

And, how to reference this class?.

I tried with

class someclass {


}

but it does not work.

like image 208
magallanes Avatar asked Nov 18 '25 14:11

magallanes


2 Answers

There are no classes in jscript. You can make an object constructor:

function someclass() {
  this.answer = 42;
}

You use it like a class:

var obj = new someclass();

To make methods you add functions to its prototype:

someclass.prototype.getAnswer = function() {
  return this.answer;
}

Usage:

var ans = obj.getAnswer();
like image 132
Guffa Avatar answered Nov 20 '25 04:11

Guffa


There are not classes as such, but here's a simple example of how to get basic object-oriented functionality. If this is all you need, great, but if you're after other features of classes, such as inheritance, someone more knowledgeable than myself will have to help.

function SomeClass(n) {
    this.some_property = n;
    this.some_method = function() {
        WScript.Echo(this.some_property);
    };
}

var foo = new SomeClass(3);
var bar = new SomeClass(4);
foo.some_method();
bar.some_property += 2;
bar.some_method();
like image 40
Seth Avatar answered Nov 20 '25 03:11

Seth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!