Tag:

我可以在JavaScript中find一个函数参数的类名吗?

第一部分 代码: function initGET(req, pre, cb){ var urlparts = req.url.split('?'); … } 问题描述: 假设我不知道什么是req / req.url,我想find更多的东西。 我知道有一个名为url的成员。 也许req是该类的实例,也许req是inheritance该类的类的实例。 题: 我可以findreq的类名吗? 因为req是一些类,并inheritance更多的类。 你必须在某个类中定义url成员。 第二部分 问题描述: 有没有在javascript中我可以find这个类,所以我可以继续看代码或文档? 例如, var http1 = require('http'); func(http1), //is there a function? I could get "http". 题: 有没有function? 我可以得到“http”。 第三方 另一个例子, 问题描述: var server = http.createServer(function(req, res) { …} http.createServer([requreListerner]) returns a […]

如何在Node.js中编写一个中间件类

我已经在Google和书籍上研究了几个小时的话题,而且我只能find非常具体的实现。 我正在努力编写一个简单的中间件类在节点JS只有普通香草的JavaScript(没有像async,co,额外的模块..)。 我的目标是了解它如何工作,而不是获得最优化的代码。 我想要一些简单的东西,比如有一个string,并通过使用中间件来添加新的string。 class上 "use strict"; class Middleware { constructor() { this.middlewares = []; } use(fn) { this.middlewares.push(fn); } executeMiddleware(middlewares, msg, next) { // This is where I'm struggling } run(message) { this.executeMiddleware(this.middlewares, message, function(msg, next) { console.log('the initial message : '+ message); }); } } module.exports = Middleware; 一个可能的用法 const Middleware = require('./Middleware'); […]

调用super()的子类javascript的错误

我正在尝试学习ES2015 JavaScript类 ,我开始这样的代码: 文件: index.js /* parent class */ class Thing { construct(){ console.log("thing constructor"); } } /* child class */ const Human = class Human extends Thing { construct(){ super(); } } let Person = new Human(); 文件: package.json { "scripts": { "serve": "nodemon index.js –exec babel-node" }, "dependencies": { "babel-cli": "^6.9.0", "babel-preset-es2015": "^6.9.0" […]

节点类中的Memoizee实例方法

我正在寻找一个优雅的方式来使用Memoizee包来记忆一个类的function。 在课堂之外,你可以轻松地做这件事: const memoize = require('memoizee') const myFunc = memoize(function myfunc(){ … }) 但在一个类块里面,这是行不通的: class foo { constructor(){ … } // Without memoization you would do: myFunc(){ … } // Can't do this here: myFunc = memoize(function myfunc(){ … }) } 我可以想到使用this.在构造函数中创build它this. 语法,但是这会导致不太一致的类定义,因为非memoized方法将在构造函数之外声明: class foo { constructor(){ // Inside for memoized: this.myFunc = memoize(function myfunc(){ […]

Javascript范围和closures,variables保持其价值

我遇到麻烦使用类方法,基本上这是一个函数范围问题(我猜)。 JS代码是: MysqlClass.prototype.registrar = function(usuario,password,correo,socket){ var resultado; this.connection.query("SELECT usuario FROM usuarios WHERE usuario=?;",[usuario],function(err,rows,fields){ if(err){ resultado = true; } if(rows.length==1){ resultado = true; }else{ resultado = false; } }); console.log(resultado); } console.log(resultado)显示undefined当我想显示true或false 我使用NodeJS和node-mysql

试图了解nodeJS和javascript对象

我试图写一个对象的快速示例,以及该对象的一些原型,并且从代码中获得了一些意想不到的结果。 我正在处理两个文件,index.js和cards.js。基本的想法是实例化一个名为“deck”的新对象,然后调用该对象上的某些方法。 这是我的代码 cards.js //constructor function cardsClass() { this.cards = [ { suit: 'heart', number: 1, name: 'ace' }, { suit: 'heart', number: 10, name: 10 }, { suit: 'heart', number: 11, name: 'jack' }, { suit: 'heart', number: 12, name: 'queen' }, { suit: 'heart', number: 13, name: 'king' } ]; } //class method […]

node.js – 在超类中获取ES6子类的文件名

我目前正在开发一个node.js(v5.10.1)应用程序,它使用ES6类语法来进行模块展示,尽pipe我不确定这是否与我正面临的问题特别相关。 假设我有以下目录结构: / |-command/ | |-walk/ | | |-config.json | | |-walk.js | | | |-eat/ | |-config.json | |-eat.js | |-core/ | |-app.js | |-command.js | |-config.json | |-config.js | |-job.js | |-module.js | |-job/ | |-breathe/ | |-config.json | |-breathe.js | |-init.js 位于每个.js文件中的类遵循以下inheritance: Config |-App |-Module |-Command | |-Eat | |-Walk | |-Job […]

在ES6中导入和扩展一个类的最好方法

我想在一个文件中创build一个类,在另一个文件中导入和扩展它。 就像是: /* someclass.js */ export class SomeClass {} /* somesubclass.js */ import SomeClass from './someclass' class SomeSubClass extends SomeClass {} 是否有可能或有没有更好的方式来做到这一点(例如使用node.js module.exports )?

如何添加mixin ES6的JavaScript类?

在具有一些实例variables和方法的ES6类中,如何添加一个mixin? 我在下面给出了一个例子,虽然我不知道mixin对象的语法是否正确。 class Test { constructor() { this.var1 = 'var1' } method1() { console.log(this.var1) } test() { this.method2() } } var mixin = { var2: 'var2', method2: { console.log(this.var2) } } 如果我运行(new Test()).test() ,它会失败,因为类中没有method2 ,因为它在mixin中,所以我需要将mixinvariables和方法添加到类中。 我看到有一个lodash mixin函数https://lodash.com/docs/4.17.4#mixin ,但我不知道如何使用它与ES6类。 我很好地使用lodash的解决scheme,甚至没有库提供mixinfunction的纯JS。

扩展EventEmitter时如何解决这个问题没有定义?

以下代码失败: var EventEmitter = require('events'); class Foo extends EventEmitter{ constructor(){ this.name = 'foo'; } print(){ this.name = 'hello'; console.log('world'); } } var f = new Foo(); console.log(f.print()); 并打印错误 this.name = 'foo'; ^ ReferenceError: this is not defined 但是,当我不扩展EventEmitter它工作正常。 为什么会发生这种情况,我该如何解决? 运行nodejs 4.2.1