Tag: 原型inheritance

TypeError:超级expression式必须为null或者一个函数,而不是用Babeljs定义的

我目前正在尝试在ES6中使用node.JS和Babel进行多文件inheritance(我使用Babel将ES6中的代码转换为ES5,因为Node现在不实现ES6)。 我正在使用导入/导出来“链接”不同的文件。 其实我有: 父类(文件1) export class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + this.x + ', ' + this.y + ')'; } } 和: 子类(文件2) import Point from './pointES5' export class ColorPoint extends Point { constructor(x, y, color) { super(x, y); this.color = color; } […]

在sails.js控制器中调用超级方法

当我在sails.js中创build一个控制器并重新定义了一些标准方法时,如何调用此控制器的默认父级方法? module.exports = { create: function(req, res) { //test some parameters if (condition) { //call regular super method, proceed as usual //_super(); <- how to do this? } else { //do some other things } } };

在ES6类中使用EventEmitter

我正在尝试在ES6中运行我自己的类中的EventEmitter: "use strict"; const EventEmitter = require('events'); class Client extends EventEmitter{ constructor(token, client_id, client_secret, redirect_uri, code){ super(); this.token = token; this.client_id = client_id; this.client_secret = client_secret; this.redirect_uri = redirect_uri; this.code = code; } eventTest(){ this.emit("event"); console.log(this.token); } } let testClient = new Client(1,2,3,4,5); testClient.eventTest(); testClient.on('event', () => {console.log('triggerd!')} ); 但事件没有做什么^^ 没有ES6我得到它与这个代码的工作: var util = require('util'); […]

节点coffeescript类文件和inheritance

我有2个类文件: foo.coffee:class class Foo bar.coffee:class class Bar extends Foo 我如何定义这些类,使它们在全球范围内可用? 我得到Bar中的错误, Foo没有定义。 我有一个index.js文件,我调用node来运行脚本。 这里是index.js的内容,我很可能也做了这个错误: exports.Foo = require("./foo") exports.Bar = require("/bar")

如何添加块使用玉模板父

我试图用玉模板创build一个模块化的布局。 我想从一个孩子的脚本块添加到它的父母父母。 我不太确定它是否可能。 这是我的结构 layout.jade head.jade index.jade users.jade layout.jade:doctype html#html头部 body block content head.jade: head title= title link(rel='stylesheet', href='/stylesheets/style.css') block scripts index.jade: extends layout block content h1 Hello include users users.jade block append scripts script(src='/javascripts/user.js') ul each user, i in users li(class=i+"-"+user) #{user} 我希望的html输出应该是: <!DOCTYPE html> <html id="html"> <head> <title>Index</title> <link href="/stylesheets/style.css" rel="stylesheet"> <script src="/javascripts/user.js"> […]

理解javascriptinheritance和node.js utilinheritance函数的例子

我有两个模块: apirequest.js和feed.js。 当我在apirequest中调用feed.start()时, TypeError: Object #<Feed> has no method 'start' 。 为什么是这样? 不是util.inherits(Feed, APIRequest); inheritanceAPIRequest的属性? apirequest.js var EventEmitter = require('events').EventEmitter; var util = require('util'); function APIRequest(endpoint) { } APIRequest.prototype.start = function() { } util.inherits(APIRequest, EventEmitter); module.exports = APIRequest; feed.js var util = require('util'); var APIRequest = require('../lib/api_request'); function Feed(endpoint) { APIRequest.call(this, endpoint); } util.inherits(Feed, APIRequest); […]

Node.js中的函数构造函数是什么?

在浏览器(至lesschrome)函数是Function实例 setTimeout instanceof Function // true 但是在节点中,它们不是 setTimeout instanceof Function // false 那么setTimeout的构造Function什么Function呢?

如何在NodeJS中为自定义错误类获取正确的回溯?

我对于在JavaScript中创build自定义Error类的“正确”方式的理解如下所示: function MyError(message) { this.name = "MyError"; this.message = message || "Default Message"; } MyError.prototype = new Error(); MyError.prototype.constructor = MyError; (代码片段从https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error中唤醒。) 使用NodeJS,如果我尝试检查这种types的错误,如: var err = new MyError("whoops"); assert.ifError(err); …回溯将显示我在编译时创build的Error对象的上下文,作为MyError的原型,而不是我用“new MyError()”创build的MyError对象。 有什么方法可以得到正确的回溯数据,而不是原型?

在mongoose的inheritance

你好,我需要在mongoose库inheritance我的模式。 有完整的插件吗? 或者我应该怎么做呢? 我还需要从Base模式inheritance所有的pre,post,init中间件。

如何扩展Javascriptdate对象?

我试图subclass /扩展本地的date对象,而不修改本地对象本身。 我试过这个: var util = require('util'); function MyDate() { Date.call(this); } util.inherits(MyDate, Date); MyDate.prototype.doSomething = function() { console.log('Doing something…'); }; var date = new MyDate(); date.doSomething(); console.log(date); console.log(date.getHours()); 和这个: function MyDate() { } MyDate.prototype = new Date(); MyDate.prototype.doSomething = function() { console.log("DO"); } var date = new MyDate(); date.doSomething(); console.log(date); 在这两种情况下, date.doSomething()起作用,但是当我调用任何本地方法(如date.getHours()或console.log(date) ,我得到'TypeError:这不是Date对象。 有任何想法吗? […]