简体   繁体   English

使用`this` vs Object.create(prototype)

[英]Using `this` vs Object.create(prototype)

I've been using JSLint to help me relearn how to code properly. 我一直在使用JSLint帮助我重新学习如何正确编码。 The program doesn't like using this in my code, so I've settled on using self=Object.create(MyType.prototype) . 该程序不喜欢在我的代码中使用this ,因此我决定使用self=Object.create(MyType.prototype) Are there any pitfalls or limitations for using this pattern? 使用此模式是否有任何陷阱或限制? Am I just being too verbose by avoiding this ? 通过避免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. 经过长时间的裁员后,我再次自学了JavaScript。 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. 我可以帮忙。

The JSLint tool only tells you how Douglas Crockford wants you to use JavaScript. JSLint工具仅告诉您Douglas Crockford如何让您使用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. JSLint这个名称颇具误导性,因为它没有根据语言标准验证代码,而是根据Douglas Crockfords关于如何使用该语言的观点来验证代码。 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. 使用JavaScript的方式有很多,不仅只有一种。

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) { };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM