Tag: oop

可以调用模块或类在node.js / Javascript中截获

在PHP中,每个类都包含一个“magic”__call函数。 使用这个可以dynamic地截取所有对类的调用。 例如使用 class TestClass { public function __call($functionname, $arguments) { .. functionname called } } 见http://www.php.net/manual/en/language.oop5.overloading.php#object.call 在JavaScript / Node.js中有类似的可能吗? 要么在一个模块(按需求加载)或类? 更新:感谢所有的评论。 这在纯JavaScript中似乎不可行。 至less目前。 正如bfavaretto指出的,这可以通过在Node.js中启用和声代理来完成。 请参阅在nodejs中启用Harmony代理 。 这个问题似乎是调用JavaScript对象时获取通知的重复。 可能相关: __noSuchMethod__ ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/noSuchMethod ),标记为非标准。

帮助检查所有参数node.js – sails – express

我想检查我的控制器中的参数。 目前,我正在这样做: create: function (req, res) { if(req.params.label && req.params.password){ // do stuff } } 但我想更快地做到这一点。 我想知道是否没有已经存在的工具。 我想要做这样的事情: create: function (req, res) { checkParams({label: {empty: false}, password: {empty: false}}, function(err){ // Do stuff }); } 你知道什么可以帮助我吗? 谢谢。

Javascriptinheritance使用节点

我想在我的子类中使用父类的方法。 在传统的面向对象中,只需要扩展你的子类就可以利用你父母的function,这是否可以使用原型? 这是我的文件结构: Parent.js var Parent = function(){ this.add = function(num) { return num + 1; }; }; module.exports = Parent; Child.js var Parent = require("./parent.js"), util = require("util"); var Child = function() { this.sum = function(num) { // I want to be able to use Parent.add() without instantiating inside the class // like this: […]

使用OOP扩展Node.js模块

我是否错过了某些东西,还是仅仅是不可能扩展像Java类那样的任意Node模块? 具体例子: 我需要passport-remember-me来将req对象暴露给_issue方法。 我试图做的是扩展该function( RememberMe.Strategy ),修改_issue函数,然后委托给原始父类的函数为实际的业务逻辑: // 1: Extend RememberMeStrategy function IWillRememberYou (options, verify, issue) { RememberMeStrategy.call(this, options, verify, issue); } util.inherits(RememberMeStrategy, IWillRememberYou); // 2: Override some method IWillRememberYou.prototype.authenticate = (req, options) => { // Save original function const issue = this._issue; // Wrap the supplied callback so it can now be sent extra args […]

将属性添加到node.js中的嵌套接口

在一个名为“some-file.ts”的文件中,我想这样做: global.someProp = someVar; 而global是在node.d.ts中定义的NodeJS.Globaltypes的预定义对象。 我怎样才能使这个接口部分? 我已经尝试了以下所有无效: interface Global { someProp: string; } global.someProp = someVar; interface NodeJS.Global { someProp: string; } global.someProp = someVar; namespace NodeJS{ interface Global { someProp: string; } } global.someProp = someVar; 我不断收到: TS339:“全局”types中不存在属性“someProp” 我该如何解决?

在JS中处理Object的嵌套属性

您好我正在编写一个模块在NodeJS中的OOP风格。 我有多个简单的对象,其中包含原始数据和多个复杂的对象包含其他对象。 const Simple = function Simple() { this.x = 0; this.y = 0; } Simple.prototype.getArea = function() { return this.x * this.y; } const Complex = function Complex() { this.ownProp = 0; this.nestedProp = new Simple(); this.otherNestedProp = new otherSimple(); } Complex.prototype.set = function(key, value) { this[key] = value; } Complex.prototype.otherSet = function(value) { […]

导出对象中的对象

所以我一直在看这个post: Node.js module.exports的目的是什么?你如何使用它? 并一直试图在JavaScript对象的范围内理解它。 所以如果我在game.js有这个: var myObj = function() { this.function1 = function(){ … }; }; exports.myObj = myObj; 在另一个文件中,我假设我会做这样的事情: var RequiredObj = require('game.js'); 要么 var RequiredObj = require('game.js').myObj; 我迷失方向的是如何使用这个对象及其function,以及如何创build这个对象的新实例。 会是这样的吗? RequiredObj.myObj.function1(); 和 RequiredObj.myObj = new RequiredObj.myObj(); 对于我传递的对象有多大也有没有限制? 因为在我的实际脚本中,我的游戏对象是巨大的。

Coffeescript和nodeJS中的常量

我应该如何保持常量? 例如,我在Github中看到了很多代码,其中表名,行等只是在coffeescript类的方法中被硬编码。 如何进行这样的常量,如何保持它们? 以及如何保持模块之间共享的常量,枚举。 我想我应该创build单独的类只暴露常量,对不对? 最后,在分布式应用的情况下,在服务之间保持常量和枚举的方法是什么? 例如,项目可以由几个应用程序组成,这些应用程序使用套接字相互通信。 我应该创build另一个只显示常量的应用程序吗? 如果是的话,这个解决scheme的性能影响是什么?

在JavaScript / Node.js中思考OOP

我很好的理解了JavaScript中OOP和原型inheritance的概念,但是有时候我想知道如何在真实应用程序中使用这些概念。 我将以几个月前在GitHub上推出的一个简单的(istic) 联系人pipe理Web应用程序为例。 主要处理程序主要驻留function: var UserModel = require('../models/userModel.js'); var checkObjectId = new RegExp('^[0-9a-fA-F]{24}$'); var root; exports.getContacts = function(request, response) { var id = JSON.parse(request.params.user)[0]; // validate if (!checkObjectId.test(id)) { return res.status(400).json({error: 'Not a user id'}); } UserModel.findById(id, function(err, user) { if (err) { return console.log(err); } response.send(user.contacts); }); }; exports.addContact = function(request, response) { var […]

V8 / JS运行时:JavaScript窗口函数如何在没有前缀“window”的情况下使用

我不太了解JS控制台,但是我正在进入命令行编程,在开始使用外部程序的shell / node之前,最好先了解我的本地环境 我的猜测是所有的命令行语句都是with (window) eval(/* whatever user typed before hitting enter/*)来调用的with (window) eval(/* whatever user typed before hitting enter/*)但是这看起来很糟糕。 上下文肯定绑定为“窗口”,如果我console.log(this)但我不知道为什么/如何 这几乎就像用户cd到窗口对象中,将其设置为上下文