how to create a class in classic jscript?

廉价感情. 提交于 2021-01-04 09:22:33

问题


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.


回答1:


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



回答2:


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



回答3:


Most recent:

  • JavaScript/Reference/Classes

Answer from 2011:

You don't really have classes on Javascript, but you have something similar. Check this example on jsFiddle

var classA = function() {     // declaring a "class"
    this.something = "Text";  // a public class field
};

classA.prototype.b = " b ";   // a class field
classA.c = "c";               // a static field
classA.prototype.d = function(x) { // a public class method
};
classA.e = function(x){     // a public static method
};

var a = new classA();       // instantiate a class

Read more on MDC...




回答4:


Define a function with the name of the class. Any var defined within it as this.whatever will act as a class member:

function SomeClass() {
  this.a;
  this.b;
}

Then add methods to the prototype:

SomeClass.prototype.methodA = function() {
  this.a++;
}

SomeClass.prototype.methodB = function() {
  this.b++;
}

I believe you can also define methods inside the constructor like this, but I've not used this syntax for a long time.

function SomeClass {
   this.a = 0;

   // Method definition
   this.methodA = function() {
      this.a++;
   }
}



回答5:


Bear with me as I only faintly grasp this myself:

Javascript does not have classes. It has objects. To replicate a class, you create an object. To create "instances" of that "class", you duplicate that object. There are a few ways to do this. One is to create a function that returns an object, then call the function, like so:

function Car(brand, year) {
return {
brand: brand,
year: year
}
}
var modelT = Car("Ford", 1900); //or whenever the model T came out
modelT.brand == "Ford" //true
modelT.year == 1900; //true 

Another way is to create an object that is a function and create a "new" of that object. The catch is you have to use new, and that's rather misleading.

var Car = function(make, year) {
    this.make = make;
    this.year = year;
};
var x =  new Car("ford", 1990);
//same tests hold true from earlier example

In my opinion the best way is to use Object.create because it best represents what's actually happening:

var Car = (function() {
    var self = Object.create({}, {
        make: {
value: "Ford"
}
year: {
value: 1900
}
    });
    return self;
})();
var modelT = Object.create(Car, {
                        make: {
                            value: "Model T"
                        },
year: {
value: 1901
}
                    });

Unfortunately, this is extremely cumbersome.

Edit:

This is an extremely helpful resource: JavaScript "classes"




回答6:


Classes in Jscript (.Net?): http://www.functionx.com/jscript/Lesson05.htm

And Powershell supports it (as well as VisualBasic, even F#). Now I see the question says not in .net. But in my defense, I was googling jscript classes, and this is where I landed.

Add-Type @'
class FRectangle {
  var Length : double;
  var Height : double;
  function Perimeter() : double {
      return (Length + Height) * 2; }
  function Area() : double {
      return Length * Height;  } }
'@ -Language JScript


[frectangle]::new()


Length Height
------ ------
     0      0


来源:https://stackoverflow.com/questions/6399229/how-to-create-a-class-in-classic-jscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!