Reputation: 2888
I was about to try out do some OOP code in js, nothing fancy just wanna see how it works. I read up on some documentations on the web, but the code gives me error in jslint. I never used jslint so im not really sure how crucial the error messages are and I hope you guys can help.
function mainClass(arg1, arg2) {
"use strict";
this.property1 = arg1;
this.property2 = arg2;
this.printClass = function printClass() {
return this.property1 + " " + this.property2;
}}
thats a simple enough js class but I get some errors and the errors are:
ln5 Strict violation. this.property1 = arg1;
ln6 Strict violation. this.property2 = arg2;
ln8 Strict violation. this.printClass = function printClass() {
ln12 Expected ';' and instead saw '}'.
So appareantly the errors are that I used this in a global context, as I read on some other posts, but I don't know how im supposed to go about it to get it fixed.
Is this not a correct way of writing a js class?
UPDATE!
var mainClass = function(arg1, arg2) {
'use strict';
this.property1 = arg1;
this.property2 = arg2;
this.printClass = function printClass() {
return this.property1 + ' ' + this.property2;
};};
I updated the code to the code above, and it works just like the other code, is there any diffrence I should be aware of declaring a class like this and the way above? And this one validate aswell.
Upvotes: 0
Views: 197
Reputation: 12341
Yeah, JSHint is a bit strict on stuff. But your code is fine, it validates perfectly when you indent it properly. Also, you were forgetting a ;
at the end of one of the function declarations:
var foo = function (arg1, arg2) {
'use strict';
this.property1 = arg1;
this.property2 = arg2;
this.printClass = function printClass() {
return this.property1 + ' ' + this.property2;
};
};
Or use the validthis
flag, which suppresses warnings about possible strict violations when the code is running in strict mode and you use this in a non-constructor function.
Upvotes: 1