Search code examples
javascriptjscript

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.


Solution

  • 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();