admin管理员组文章数量:1278819
how to create a class in classic jscript? (not jscript)
And, how to reference this class?.
I tried with
class someclass {
}
but it does not work.
how to create a class in classic jscript? (not jscript)
And, how to reference this class?.
I tried with
class someclass {
}
but it does not work.
Share Improve this question edited Oct 30, 2020 at 21:27 Foad S. Farimani 14k18 gold badges93 silver badges239 bronze badges asked Jun 18, 2011 at 22:18 magallanesmagallanes 6,8544 gold badges60 silver badges56 bronze badges 5- 1 Javascript doesn't have classes. And you don't need them. What are you really trying to do? – user395760 Commented Jun 18, 2011 at 22:20
- 2 You should read developer.mozilla/en/JavaScript/Guide/… – Felix Kling Commented Jun 18, 2011 at 22:27
- 1 it is not javascript but jscript – magallanes Commented Jun 18, 2011 at 22:35
- 3 JScript is Microsoft's implementation of ECMAScript. JavaScript was originally developed Netscape, adapted by Microsoft as JScript. Later, JavaScript was standardized as ECMAScript. en.wikipedia/wiki/ECMAScript – Felix Kling Commented Jun 18, 2011 at 22:42
- Probably the following answer might be useful for you: stackoverflow./a/11144431/476712 – lorefnon Commented Jun 21, 2012 at 18:27
6 Answers
Reset to default 7There 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./jscript/Lesson05.htm
And Powershell supports it (as well as VisualBasic, even F#). Now I see the question says not in . 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
本文标签: javascripthow to create a class in classic jscriptStack Overflow
版权声明:本文标题:javascript - how to create a class in classic jscript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741216697a2360179.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论