简体   繁体   English

实例化后,使用mixin扩展的node.js对象无法找到函数

[英]node.js object extended with a mixin can't find function after instantiation

I've managed to get a fairly complex setup (though that's a question for Code Review) for my mixins that looks like this: 我已经为我的mixins获得了一个相当复杂的设置(尽管这是代码审查的问题),如下所示:

TooManyCaps.js TooManyCaps.js

module.exports = {
  labelCopyCaps: () => {
    if (this.release.tracks.length > 1) {
      if (_this._notEnoughLowercase(this.release.title)) {
        this._recordError(release, 'LABELCOPYCAPS');
      } else {
        this.release.tracks.some( (track) => {
          if (this._lowerCaseCount(track.label_copy)) {
            this._recordError(release, 'LABELCOPYCAPS');
            return true;
          }
        });
      }
    }
  },
  _notEnoughLowercase: (str) => {
    if ((str.match(/[a-zA-Z]/g)||[]).length > 3
        && str.length - str.replace(/[a-z]/g, '').length) {
      return true;
    }
    return false;
  }
};

I then have an Object that would use this as a mixin: 然后,我有一个将其用作混合的对象:

Rule.js Rule.js

class Rule {
  constructor(release) {
    this.release = release;
    this.errors = [];
  }

  _recordError(error, options) {
    this.errors.push({
      release_id: this.release.id,
      rule: error,
      options: options,
    });
  }
}

module.exports = Rule;

i then have an index page that joins them together 然后我有一个索引页面,将它们连接在一起

index.js index.js

const TooManyCaps = require('./TooManyCaps');
const Rule = require('./Rule');
Object.assign(Rule.prototype, [TooManyCaps]);
module.exports = Rule;

And then my main start of the program that does some instantiating of things: 然后,我的程序的主要开始是进行一些实例化:

'use strict';
const RuleValidator = require('./job/validation/RuleValidatorMixin');
const Rule = require('./job/validation/rulesmixins/rules/index');

// some logic that's a loop
arr.forEach((value) => {
  new RuleValidator(new Rule(value)).validate();
}

and within validate() I have: 在validate()中,我有:

  validate() {
    console.log('VALIDATE');
    this.rule.labelCopyCaps();
    // console.log(this.rule);
  }

But then when I run this, I get: 但是当我运行它时,我得到:

this.rule.labelCopyCaps is not a function

So where have i gone wrong? 那我哪里出问题了?

Object.assign does not take an array: Object.assign不采用数组:

 Object.assign(Rule.prototype, [TooManyCaps]); // ^ ^ 

should be just 应该只是

Object.assign(Rule.prototype, TooManyCaps);

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

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