Tag: 原型

在javascript中使用更高阶函数的原型函数

我试图concat使用减less数组的数组,我想我可以使用Array.prototype.concat函数是这样的: arr = [[1],[2],[3]] arr.reduce((a, b) => Array.prototype.concat(a, b), []) 哪些工作正常,并给我的数组[1, 2, 3] 。 然后我觉得我可以变得更聪明,像这样做: arr = [[1],[2],[3]] arr.reduce(Array.prototype.concat, []) 但是,这给我一个错误: TypeError: Array.prototype.concat called on null or undefined at Array.reduce (native) at Object.<anonymous> (/home/axel/Developer/temp/reduce2.js:2:5) at Module._compile (module.js:556:32) at Object.Module._extensions..js (module.js:565:10) at Module.load (module.js:473:32) at tryModuleLoad (module.js:432:12) at Function.Module._load (module.js:424:3) at Module.runMain (module.js:590:10) at run (bootstrap_node.js:394:7) […]

Node.js原型inheritance

尝试用Javascript进行原型包装,特别是Node.js,并进行简单的testing。 function Lint() { this.input = 'foo'; events.EventEmitter.call(this); } Lint.prototype.dirs = function (dirs) { _.each(dirs, this.files); } Lint.prototype.files = function (dir) { console.log(this.input); // trying to get 'foo', returns undefined } var lint = new Lint(); lint.dirs(['js', 'js/views']); Lint.prototype.files日志未定义,因为this不是指Lint的实例。 我在这里错过了什么? 我能想到的唯一解决scheme就是将最初的this从Lint.prototype.dirs给其他函数。 我很确定有一个更好的方法。

如何去耦合类中的嵌套对象函数

背景 这是一个简单的例子,演示了我正在努力完成的事情。 我正在尝试创build一个Person(name)类Person(name) 。 这个类有一个对象say有几个与之相关的函数,例如 Person.say.name(); 当被调用时,这应该输出My name is (name provided) 简单的工作例子 // node v0.10.15 var Person = function(name) { this.name = name; }; Person.prototype.greet = function() { console.log('Hello ' + this.name); }; Person.prototype.say = function() { var self = this; this.say = { name: function() { console.log('My name is ' + self.name); } }; […]

原型和原型之间的区别

有什么区别 prototype.somefunction() 和 prototype._anotherfunction() 我知道这可能是微不足道的,但我找不到它。

对象在使用原型时没有方法

我是一种noobie在JavaScript中,当我尝试使用原型来扩展我的对象,我得到以下错误代码: Object function ProcessManager() {…} has no method 'startBrowsing' 这是我的代码。 我在nodejs中执行这个代码。 代码 function ProcessManager(){ this.browser = new Browser(); this.salePagesToVisit = []; this.salePagesCurrent = []; this.salePagesDone = []; this.categoryPagesToVisit = []; this.categoryPagesCurrent = []; this.categoryPagesDone = []; this.listPagesToVisit = []; this.listPagesCurrent = []; this.listPagesDone = []; } ProcessManager.prototype.startBrowsing = function () { winston.log('verbose', 'Starting scrapping Bazarchic'); […]

在Javascript中,为什么Object.getPrototypeOf()返回一个空列表?

在Chrome 42.0中,我使用var myArray = [1,2]为myArray赋值, 我期望Object.getPrototypeOf(myArray)会是这样的(从这里截图).. 但是,当我在REPL中评估代码时,我只有一个空的列表: 有没有人有任何想法呢?

Nodejs为什么数组初始化与原型静态?

考虑下面的代码 Class = function() { //this.array = []; } Class.prototype.array = []; Class.prototype.str = null; var a = new Class(); var b = new Class(); a.array.push("a"); console.log (a.array); b.array.push("b"); console.log (b.array); a.str = "a"; console.log (a.str); console.log (b.str); b.str = "b"; console.log (a.str); console.log (b.str); 如果我们按照原样运行它,我们会注意到在其任何实例中修改Class的array都是静态完成的 – 对a中数组的更改反映在b ,反之亦然。 然而,修改strvariables,尽pipe初始化与array相同的方式不是静态的。 如果我们取消注释构造函数中的行,那么在任何情况下对数组的更改都不会静态地完成。 我偶然发现了一个nodejs错误? 我无法在任何地方find有关这种情况的详细信息,那么是否有人可以解释为什么nodejs有这种行为?

节点 – this.func()不是一个函数

function Job(name, cronString, task) { "use strict"; this.name = name; this.cronString = cronString; this.isReady = false; this.task = task; } Job.prototype.performTask = (db, winston) => { "use strict"; const Promise = require("bluebird"); let that = this; return new Promise((resolve, reject) => { let output = ""; let success = true; try { output = that.task(); […]

将函数追加到JavaScript Object Literal的__proto__属性是否是一个好主意?

我有一个对象文字: var tasks = {}; 我基本上增加了这样的东西: function addTask(task) { tasks[task.id] = task } 我想修改,以便我可以调用每个任务的startfunction。 所以: var tasks = {}; tasks.__proto__.start = function(key) { // do stuff with this[key] } function addTask(task) { tasks[task.id] = task tasks.start(task.id) } 我听说最好避免原始对象,并减缓执行速度。 不过我不会重新分配它,我正在追加它。 有没有其他更好的select?

TypeError:app.get不是一个函数

我试图在原型中使用快递 function ioServer() { } module.exports = ioServer; ioServer.prototype.start = function() { var app = require('express') var http = require('http').Server(app) var io = require('socket.io')(http) app.get('/', function(req, res) { var outPut = "" res.sendFile(__dirname + './client/index.html') }) http.listen(3000, function(port) { console.log('Listening on port, ' + 3000) }) } 但是,当我使用它引发错误TypeError: app.get is not a function当我删除它的原型部分的作品。