开发者

how to create a class in classic jscript?

开发者 https://www.devze.com 2023-03-14 02:04 出处:网络
how to create a class in classic jscript? (not jscript.net) And, how to reference this class?. I tried with

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.


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


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


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...


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++;
   }
}


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"


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号