0

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.

Björn Hjorth
  • 2,459
  • 5
  • 25
  • 31

1 Answers1

1

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.

Community
  • 1
  • 1
federico-t
  • 12,014
  • 19
  • 67
  • 111
  • Good catch on the ; stupid mistake, but the first 3 errors are still there even with proper indenting. – Björn Hjorth Aug 08 '13 at 22:26
  • @BjörnHjorth I just realized that you were using JSHint not JSLint. This should validate, though you're probably going to get a `'foo' is defined but never used.` error. – federico-t Aug 08 '13 at 22:35
  • I'm using Adobe Edge Code that has built in JSLint, I dont know what JSHint is. – Björn Hjorth Aug 08 '13 at 22:41
  • I updated the first post with new code and that one works, what is the diffrence between that and the other? – Björn Hjorth Aug 08 '13 at 22:46
  • Both declarations are _almost_ synonymous (read http://www.dustindiaz.com/javascript-function-declaration-ambiguity/), but with the var "shorthand" version JSHint doesn't throw a warning. – federico-t Aug 08 '13 at 23:00
  • Thanks for your time you have helped me solve the problem. much appreciated. – Björn Hjorth Aug 08 '13 at 23:43