Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 491 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to create a class in classic jscript?

#1
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.
Reply

#2
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:

[To see links please register here]

Reply

#3
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();
Reply

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

#5
Classes in Jscript (.Net?):

[To see links please register here]


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
```
Reply

#6
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();
Reply

#7
Most recent:

- [JavaScript/Reference/Classes](

[To see links please register here]

)

---

Answer from 2011:

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

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...][2]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through