Tag: 这个

Javascript分配中的左手边无效

在尝试向nodejs的Buffer类添加队列typesfunction时,我构build了以下函数: Buffer.prototype.popleft = function(n) { tRet = this.slice(0,n); this = this.slice(n,this.length-1); // error here return tRet; }; 但是,此代码会产生以下错误:“ReferenceError:在赋值中无效的左侧” 我知道这个问题是在函数内部赋值“this”,我不知道是完成这种相同types逻辑的更好方法。 编辑: 结束在缓冲区周围写一个对象,如下所示: sPort.vBuffer = {} sPort.vBuffer.buff = new Buffer(0); sPort.vBuffer.off = 0; sPort.vBuffer.expect = -1; sPort.vBuffer.ReadChr = function() { this.off ++; return this.buff[this.off – 1]; }; sPort.vBuffer.ReadUInt32LE = function() { this.off += 4; return this.buff.readUInt32LE(this.off – […]

在nodejs模块中,“this”是什么意思?

我有一个简单的代码,如下所示,并将其作为节点模块执行: console.log(this); module.exports = {…}; 我知道global是默认的上下文(如浏览器中的window ),但是this关键字是指什么?

node.js EventEmitter导致范围问题

当使用nodejs事件系统时,我遇到了一个恼人的问题。 如下面的代码所示,当侦听器捕获一个事件时,事件发射器对象在callback函数中拥有“this”而不是侦听器。 如果将callback放在侦听器的构造函数中,这不是一个大问题,因为除了指针“this”之外,还可以使用构造函数作用域中定义的其他variables,如“self”或“that”。 但是,如果将callback放在构造函数之外(如原型方法),则在我看来,没有办法获得侦听器的“this”。 不太确定是否有其他解决scheme。 另外,为什么nodejs事件发出使用发射器作为监听器的调用者安装有点困惑? util = require('util'); EventEmitter = require('events').EventEmitter; var listener = function () { var pub = new publisher(); var self = this; pub.on('ok', function () { console.log('by listener constructor this: ', this instanceof listener); // output: by listener constructor this: false console.log('by listener constructor self: ', self instanceof listener); // […]

V8 javascript:chrome vs node.js – “this”上下文的区别

看看下面的代码: var pocket = { cash: 1000, showCash: function() { return this.cash; } }; var thomas = { name: "Thomas", work: function() { console.log('I don\'t want to, but I have to… work!'); }, cash: 5000 }; var bird = { fly: function() { console.log('I\'m flying!'); } }; console.log(pocket.showCash()); thomas.showCash = pocket.showCash; console.log(thomas.showCash()); bird.showCash = pocket.showCash; […]

在文件之间共享“这个”

我正在创build一个有很多方法的对象,并试图避免我的文件非常长。 问题是一些方法引用对象中的其他信息。 我想能够做到这样的事情: index.js var User = function(first, last){ this.firstname = first; this.lastname = last; }; User.prototype.name = require('./methods/name.js') 方法/ name.js module.exports = { full: function(){ return this.firstname + " " + this.lastname; }, formal: function(){ return "Mr. " + this.lastname; } }; 这是有道理的,为什么this在这种情况下不起作用,但有没有一种不同的解决scheme可以引用其他文件? 唯一我能想到的是使用fs和eval()而不是require,但是对我来说这似乎是一个破解,或者显而易见有一个长文件。 有什么更好的吗? 我打算在原型上有大约35个物体,每个物体上平均有4种方法。 build议? 谢谢。

词汇这在consis方法在Javascript中

下面是我的解释代码。 我有一个类的成员函数和variables如下。 functionOne&functionTwo简洁的方法。 function MyUtils() { this.actions = { functionOne(param1, param2) { console.log('This is first function which is not in action'); }, functionTwo(param1, param2) { console.log('This is second function calling function three'); //HOW DO I CALL functionThree HERE? // I tried doing – this.functionThree() but not working } } this.functionThree() { console.log('This is third function'); […]

节点 – 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(); […]

如何改变ES6箭头函数的“this”指向的是什么?

在traverse npm包中有这个例子 var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ]; traverse(obj).forEach(function (x) { if (x < 0) this.update(x + 128); }); 在callback函数中你可以调用this.update 。 我知道在这种情况下,你应该使用常规(不是ES6箭头)风格的函数定义,如上所示。 但出于好奇,你将如何使用ES6箭头函数语法的代码工作? 如果我尝试如下,我得到TypeError: Cannot read property 'update' of undefined因为当然this是不一样的上面。 traverse(obj).forEach((x) => { if (x < 0) this.update(x + […]

如何从一个对象中访问“this”

我有一个这样的js对象: let service = function () { this.a = 100; } service.prototype.func = function() { console.log(this.a) } service.prototype.func2 = function () { console.log('func2') this.func(); } service.prototype.obj = { m: { n: { o: function() { this.func2(); }, p: service.prototype.func2.bind(service.prototype) } } } 我想从o或p访问'a',这里是代码: let s = new service(); console.log(sa) s.func() s.func2() s.obj.mnp() s.obj.mno() 和输出是 100 […]

“this”这个关键字是如何工作的?

我注意到,似乎并没有清楚的解释this关键字是什么,以及它是如何正确(和不正确)在堆栈溢出网站的JavaScript中使用。 我目睹了一些非常奇怪的行为,不明白为什么会发生。 这是如何工作的,什么时候应该使用?