Tag: es6 class

从类未定义的构造函数中的新对象

我从构造函数中的类创build一个新的对象,每当它运行时,我得到一个错误,操作未定义的方法,虽然它在构造函数中定义。 操作本身是经过彻底的testing,并在单独的环境中工作,所以这不是问题。 我使用Babel构build它, 而不是直接在Node 7.0.0中运行它 import Operate from "./operate" export default class { constructor(Schema) { this.schema = Schema this.operate = new Operate(this.schema) console.log(this.operate.run) // <- Logs just fine } update(req, res) { console.log(this.operate.run) // <- Nada this.operate.run(req.body) .then(value => { res.status(200).json(value) }) } 这感觉就像我错过了一些根本的东西。 我听说这不是一个伟大的模式,所以请随时提出一个更好的方法。 非常感谢。 更新:这是如何使用更新。 我不怀疑这里有任何问题,因为它已经工作得很好,当我从另一个模块导入控制器作为一个function,而不是一个类 import {Router, } from "express" import Controller […]

无效的定位器错误

我试图在页面对象样式中重写我的testing,但出现了一些问题。 我使用类选项卡,这是我的代码的一部分: var World = require('../support/world.js'); const isAllAjaxRequests = require('../scripts/util').isAllAjaxRequests; const isElementLocatedAndVisible = require('../scripts/util').isElementLocatedAndVisible; module.exports.Tab = class Tab { constructor(data) { this.name = "Base"; this.locators = { 'nextStepIsLocked': {xpath: '//md-tab-item[@aria-selected="true"]//div[@class="cc-status red"]'}, 'isActiveTab': {xpath: '//md-tab-item[@aria-selected="true"]//span[text()="'+ data + '"]'} } } waitForElement(bySelector) { var driver = World.getDriver(); var self = this; //var bySelector = self.locators[bySelector]; return driver.wait(isAllAjaxRequests(driver), […]

出口承诺链的快速路线方法的最佳途径?

我有一个正在被重构使用ES6承诺避免callback地狱的API路线。 在成功转换为承诺链后,我想将我的.then()函数导出到单独的文件以保持清晰和清晰。 路线文件: 函数文件: 这工作正常。 然而,我想要做的是将在类的constructor()函数中声明的函数移动到独立的方法,它可以引用由构造函数实例化的值。 这样一切都更好。 但是,当我这样做时,我遇到了范围问题 – this是没有定义,等等。这样做的正确方法是什么? ES6适合在这里使用,还是应该使用其他的结构? 原始代码: 路线… .post((req, res) => { let SubmitRouteFunctions = require('./functions/submitFunctions.js'); let fn = new SubmitRouteFunctions(req, res); // ******************************************* // ***** THIS IS WHERE THE MAGIC HAPPENS ***** // ******************************************* Promise.all([fn.redundancyCheck, fn.getLocationInfo]) .then(fn.resetRedundantID) .then(fn.constructSurveyResult) .then(fn.storeResultInDB) .then(fn.redirectToUniqueURL) .catch((err) => { console.log(err); res.send("ERROR SUBMITTING YOUR RESULT: ", […]

TypeError:<class>不是一个构造函数,但不同的类可以正常工作

我得到一个错误,我可以实例化一个类,但不是其他的,但据我所知,这两者之间没有区别。 我是新来的nodejs,不知道我做错了什么。 下面显示的所有文件都是目录中的兄弟。 / ********** exampleClass.js ******** / const classOne = require("./classOne"); const classTwo = require("./classTwo") module.exports = class exampleClass { method() { // works fine const classOneInstance = new classOne(); const classTwoInstance = new classTwo(); // gives error 'TypeError: classTwo is not a constructor' UNLESS I require classTwo in the method. Doesn't matter if […]

需要外部模块作为ES6类实例variables

我有一个ES6类,这取决于一些外部模块的工作。 由于这是一个节点应用程序,我使用CommonJS来要求和加载模块。 然而,这种模块加载使得unit testing变得复杂,这并不是什么秘密。 我当然可以通过构造函数dependency injection所有必需的模块,但是这在dynamictypes语言中感觉很麻烦。 我也不喜欢使用像proxyquire这样的库,因为它膨胀了我的testing代码。 所以我想出了将所需模块存储为实例variables的想法。 例如: const someModule = require('some-module'); class MyClass { constructor() { this.someModule = someModule; } someFunction(value) { return this.someModule.someFunction(value); } } 这样我就可以使用模块加载器加载依赖关系,并在unit testing中仍然监视/存根/模拟它们。 这被认为是不好的做法,或者你可以看到任何重大的缺点?

如何正确从string内置类inheritance

我想扩展使用ES6类的内置JS String类。 即只是从stringinheritance。 但是,下面的代码不起作用。 +=运算符将myStr实际types更改为String 。 AFAIK,JS中没有运算符inheritance。 那么,如何避免这种行为呢? class QString extends String { isEmpty() { return this.length === 0; } } var testStr = new QString(); console.log(testStr.isEmpty()); testStr += new QString("abc"); console.log(testStr.isEmpty()); // 'TypeError: testStr.isEmpty is not a function' PS我知道有机会添加一个新的方法String.prototype 。 但这是一个不好的做法。 UPD :我明白JS中没有+运算符重载,原来的答案很清楚。 但是,它完全没有提到inheritance和ES6类。 我想至less应该保存被接受的答案。

单例inheritanceBuggy行为

我已经发现在使用Singleton模式的JavaScript es6inheritance中的马车行为。 代码是 : let instanceOne = null; class One { constructor() { if (instanceOne) return instanceOne; this.name = 'one'; instanceOne = this; return instanceOne; } method() { console.log('Method in one'); } } let instanceTwo = null; class Two extends One { constructor() { super(); if (instanceTwo) return instanceTwo; this.name = 'two'; instanceTwo = this; […]

在es6 javascript类的非静态成员函数中调用静态getter

我如何从ES 6类中的普通成员函数调用静态函数? 这里是一个例子: class Animal { constructor(text) { this.speech = text; } static get name() { return "Animal"; } speak() { console.log( this.name + ":"+ this.speech) } } class Tiger extends Animal { static get name() { return "Tiger" } } var animal = new Animal("hey there"); animal.speak(); var tiger = new Tiger("hello"); tiger.speak(); // […]

Javascript的ES6类variables

用Mongoose探索Javascript ES6类,并在访问类variables时遇到困难。 我想在引用在类的构造函数中声明的variables的cursor.on(data)事件中使用this.name 。 我怎样才能做到这一点? 'use strict'; const Mongo = require('../mongo') class Example { constructor() { this.name = 'Test Class'; } export(docId, callback) { console.log('In export' + docId); const cursor = Mongo.findDocById(docId); console.log(this.name); // Prints "Test Class" cursor.on('data', function (document) { console.log(document); console.log(this.name); // Prints "undefined" }); cursor.on('close', function () { Mongo.close(); callback(null, 'Success') }); […]

在只有nodejs的环境中,在ES6 Module / Class中定义“真正的”私有方法,而不会泄漏任何信息

我知道,没有真正的私人方法INSIDE ES6类。 不过,我正在玩一下,发现了一些好东西 – 也许… 正如我所说,不可能不公开对象的属性。 但是我试图实现一些OOP编程,因为我把我的类分成单独的文件,然后导出这些类,如: class MyClass { constructor() { /** * Initialize stuff… */ } myMethod() { /** * Do public stuff… */ } } // expose class to environment. export default MyClass; 所以我可以导入类: import MyClass from './MyClass.js'; 当然myMethod可以从导入模块的其他文件访问。 Sinced我需要的variables和函数只能由我尝试过的类访问: // private variable outside of class scope but still accessible. let possiblePrivateVariable […]