1

I've been using JSLint to help me relearn how to code properly. The program doesn't like using this in my code, so I've settled on using self=Object.create(MyType.prototype). Are there any pitfalls or limitations for using this pattern? Am I just being too verbose by avoiding this? Here is an example of how I've been writing constructor functions with prototype inheritance:

function MyType(args) {
    var privateVar1 = "value",
        self = Object.create(MyType.prototype);

    function privateFunction() { )

    self.publicMethod1 = function(aa) {
        // do stuff with private vars & self. Ex:
        var methodVar = privateVar1 + aa;
        self.publicProperty = args;
        privateFunction();
        return methodVar;
    };

    return self;
}
MyType.prototype = {
    publicProperty: "initial value",
    publicMethod2: function (aa) { }
}

function MyType2(args) {
    var self = Object.create(MyType2.prototype);
    return self;
}
MyType2.prototype = Object.create(MyType.prototype);
MyType2.prototype.publicMethod3 = function (aa) { };

I've teaching myself JavaScript again after a long layoff. It has been about ten years since I've done anything. The Internet never really forgets, but progress still happens. So some tutorials seem to be based on older standards/suggestions while newer articles contradict them. It is becoming confusing to sort out which articles are current and which are out-of-date. I could use some help please.

Jimmy
  • 158
  • 1
  • 10
  • 3
    For less biased feedback on your code, use http://jshint.com instead. Nothing wrong with using `this` in lots of places. You don't show what specifically jslint objected to so we can't really comment on that. – jfriend00 Oct 10 '15 at 22:36
  • 2
    Unfortunately you missed the 10 years that ECMAScript didn't change and DOM APIs changed slowly. Both are now quite dynamic, with an intention to release a new version of ECMAScript every year (hence versions are now named after the year of release, starting with ECMAScript 2015, aka ECMAScript ed 6). ECMAScript has changed dramatically from ed 3 to the current version and will continue to change quite a bit for at least the next few years. Older tutorials won't be wrong, but likely don't represent the best you can do with modern language features. – RobG Oct 10 '15 at 22:49
  • 1
    There is no problem with your pattern, but I think creating a new instance when you already had one is not a great idea. I would [tell JSLint to shut up](http://stackoverflow.com/a/30315031/1529630) and, if you want to be safe, check `this instanceof MyType`. – Oriol Oct 10 '15 at 23:50
  • Oriol, thank you sir. Could you explain why you think it is a bad idea? AFAIU, when a function, Fn, is called with `new`, `this` will be the same value as `Object.create(Fn.prototype);`. However when Fn is called without `new`, then this is undefined or the global object (or something else?). By explicitly calling `Object.create(Fn.prototype);` and returning the object, I hope that the function will always return a new object as expected. Is my reasoning correct? Why do you think it is a bad idea? – Jimmy Oct 11 '15 at 01:04
  • jfriend00, RobG, thank you too. Thanks for the heads up about jshint. I'm worried about using this wrong, but it does make code more understandable in certain cases. I just read it can also help the script use less memory if many objects are created (and functions are moved to the prototype using `this` in place of `self`). RobG, that's interesting! I have a lot of catchup to do. – Jimmy Oct 11 '15 at 01:08
  • Changing your whole pattern so that a mistake is less painful is not the best idea... – dandavis Oct 11 '15 at 01:26

1 Answers1

2

The JSLint tool only tells you how Douglas Crockford wants you to use JavaScript. The name JSLint is rather misleading, as it doesn't verify code according to the language standards, it verifies code according to Douglas Crockfords views on how to use the language. If you want to follow his advice to the letter, then you are doing it right.

Other tutorials aren't necessarily out of date or wrong, they are mostly just different. There are many different ways of using JavaScript, not only one.

Personally, I would write:

function MyType(args) {
  var privateVar1 = "value";

  function privateFunction() { )

  this.publicMethod1 = function(aa) {
    // do stuff with private vars & this. Ex:
    var methodVar = privateVar1 + aa;
    this.publicProperty = args;
    privateFunction();
    return methodVar;
  };
}

MyType.prototype = {
  publicProperty: "initial value",
  publicMethod2: function (aa) { }
}

function MyType2(args) {
}
MyType2.prototype = Object.create(MyType.prototype);
MyType2.prototype.publicMethod3 = function (aa) { };
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thank you, not only for the advice but for showing how you would do it. I appreciate it. – Jimmy Oct 11 '15 at 17:11