Tag: 原型inheritance

在Mongoose中填充inheritance的文档

我正在尝试为以下模型创build一个数据库模式: 我不知道什么是更好的方式来表示这个在MongoDb将是,但因为我使用Mongoose和有一个插件的inheritance,我想下面的: var mongoose = require('mongoose') , extend = require('mongoose-schema-extend') , Schema = mongoose.Schema , ObjectId = mongoose.Schema.Types.ObjectId var db = mongoose.connection; db.on('error', console.error.bind(console, 'connection error:')); db.once('open', function callback () { //Mashup and Item are bound (itemSchema is a sub-doc) var itemSchema = new Schema({ pos: { top: Number, left: Number } , size: { […]

在这种情况下使用Javascript的原型inheritance有什么好处吗?

假设我在Javascript(NodeJS)中有两个场景: 情况1: var obj = {}; obj.getAll = function(..) {…}; obj.getOne = function(..){…}; module.exports = obj; 我在调用这个模块的代码中使用obj 。 VS的东西沿线: 情景2: var constructor = function(){}; constructor.prototype.findAll = function(..){…}; constructor.prototype.findOne = function(..){…}; module.exports = constructor; 在调用这个模块的代码中,我根据需要从constructor创build一个对象并使用它。 scheme2与scheme1相比有什么优势?

node.jsinheritance喜欢子构造函数属性

var util = require('util'); function Entity(){ //this.x == 10 at this point this.x = 0; this.y = 0; this.globalInit(); } Entity.prototyp.globalInit = function(){ console.log("this.x ", x); }; function Actor(){ this.x = 10; this.y = 10; Actor.super_.apply(this, arguments); } util.inherits(Entity, Actor); var a = new Actor(); //outputs -> this.x 0 我有这两个构造函数。 我想要在子构造函数中定义的属性是最终的属性。 我可以将Actor.super_.apply移动到构造函数的顶部,但是有一个初始化逻辑( globalInit ),我想保留在父构造函数的末尾

如何从子模型获取父表的属性

var ParentModel = bookshelf.Model.extend({ }); var ChildModel = ParentModel.extend({ }); new ChildModel().fetch().then(function(child){ console.log(child.get('parentAttribute')); //undefined }); 我怎样才能从子对象获得inheritance的表属性?

给我一个Object#<Object>的NodeJ没有方法

我有一个类,其辅助类定义: function ClassA(){ this.results_array = []; this.counter = 0; this.requestCB = function(err, response, body){ if(err){ console.log(err); } else{ this.counter++; var helper = new ClassAHelper(body); this.results_array.concat(helper.parse()); } }; }; function ClassAHelper(body){ this._body = body; this.result_partial_array = []; this.parse = function(){ var temp = this.parseInfo(); this.result_partial_array.push(temp); return this.result_partial_array; }; this.parseInfo = function(){ var info; //Get some info […]

Node.js模块的inheritance

我在node.js中有inheritance问题 我遵循从现有的线程stackoverflow模式,但我的代码仍然不能像它应该。 让我们从两个项目开始,第一个'base.js': function Base() { this.type = 'empty'; } Base.prototype.getType = function () { return this.type; } module.exports = Base; 然后我有我的“second.js”文件,它应该inheritance基地 var Base = require('./base.js'), util = require('util'); function Second() { Base.apply(this, arguments); } util.inherits(Second, Base); Second.prototype.getData = function () { return 12; } module.exports = Second; 在我的app.js中我打电话 var second = new require('./second.js'); console.log(second.getType()); […]

Object.create(BaseObject)和util.inherits(MyObject,BaseObject)有什么区别?

这两个解决scheme设置原型有什么区别? MyObject.prototype = Object.create(EventEmitter.prototype); MyObject.prototype = util.inherits(MyObject, EventEmitter); UPDATE 是啊,我看到它在多个项目中,他们将原型构造函数设置(恢复)到实际的对象构造函数,如下所示: MyObject.prototype = Object.create(EventEmitter.prototype); MyObject.prototype.constructor = MyObject; “恢复”构造函数背后的原因是什么? 我使用引号,因为这个动作看起来更像是一个覆盖,因为MyObject.prototype是EventEmitter那么原型的构造函数应该是EventEmitter的构造EventEmitter 。 恢复原型构造函数的优点是什么? 这真正的生活问题解决了什么? 其次是超级属性中的基础构造函数的优点是什么?

节点模块和原型inheritance

目前我有: index.js(在模块foo中) function Foo(options) { this.memberVar = options.memberVar; } Foo.prototype.bar = function(word) { console.log('word: ', word); } module.exports = Foo; server.js(在单独的模块栏中) var Foo = require('foo'), foo = new Foo(options), greeting = 'Hello World!'; foo.bar(greeting); // prints Hello World! 这很好,除了我觉得如果我不需要使用new关键字来实例化一个新的foo对象来暴露它的成员函数,它可能会更漂亮,更容易理解。 所以这是我想要做的: var greeting = 'Hello World!', foo = require('foo')(options); foo.bar(greeting); // prints Hello World! 如何修改我当前的foo – […]

NodeJS中的模块inheritance

我试图在NodeJS应用程序中实现一些function,其中一些控制器(模块)将从一个低音模块inheritance,以便在其中存储所有常用function。 我很难做到这一点,我不确定为什么。 这是我到目前为止: 我的基本模块(其他人将从中inheritance): var http = require('http'); function ApiBaseController() { var self = this; } ApiBaseController.prototype.makeRequest = function() { }; module.exports = ApiBaseController; 一个子模块(将inheritance上面的一个模块: var APIBaseController = require(__dirname + '/apiBase/ApiBaseController.js'), inherits = require('util').inherits; function JustGivingAPI() { APIBaseController.apply(this); } inherits(APIBaseController, JustGivingAPI); JustGivingAPI.prototype.getAmount = function() { console.log("here"); }; module.exports = JustGivingAPI; 以及需要该模块的客户端代码: var express = require('express'); […]

使用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 […]