从事件发射器设置实例变量
Setting instance variable from event emitter
所以当我调用下面的代码name的输出值总是"testname"的默认值。应该是"newvalue";
// Constructor
function test(connection) {
this.name = 'testname';
}
test.prototype.exec = function () {
var Request = require('tedious').Request // this could be any event emitter;
request = new Request("select id, name from somevals where id = 1", function (err, rowCount) {
if (err) {
console.log(err);
} else {
console.log(rowCount + ' rows');
}
});
this.connection.execSql(request);
request.on('row', function (columns) {
this.name = 'newvalue'; //how do i set the instance variable name so that it is visible to the calling module
});
};
// export the class
module.exports = test;
调用看起来像
var test = require("./Model/test");
var b = new test(connection);
b.exec();
console.log(b.name);
exec接受回调
// Constructor
function test() {
this.name = "testname";
}
test.prototype.exec = function (callback) {
var Request = require('tedious').Request // this could be any event emitter;
request = new Request("select id, name from somevals where id = 1", function (err, rowCount) {
if (err) {
console.log(err);
} else {
console.log(rowCount + ' rows');
}
});
this.connection.execSql(request);
request.on('row', function (columns) {
callback(columns[1]); //how do i set the instance variable name so that it is visible to the calling module
});
};
// export the class
module.exports = test;
回调定义和测试调用如下
function callback(name) {
console.log(name);
}
function executeStatement() {
var beatle = require("./Model/test");
var b = new test(connection);
b.exec(callback);
console.log(b.name);
}
相关文章:
- 向模板实例变量传递调用方法调用的结果时出现异常
- Rails/Javascript:将实例变量传递给Coffeescript时发生Uncaught ReferenceEr
- 访问 javascript 对象的实例变量
- Angular2/Typescript:从链接可观察函数访问实例变量
- 如何在Promise回调中访问实例变量
- 如何使用每个ajax请求更新实例变量
- 实例变量变为未定义 - CoffeeScript
- 如何显式创建实例变量
- 如何在 JavaScript 中创建实例变量
- 模块化模式中的私有实例变量
- 将实例变量传递给 js Rails
- 找出实例变量名称本身
- 从视图中访问另一个操作实例变量
- 从视图访问实例变量
- 在主干模型中定义实例变量的正确方法是什么
- 是否可以在 javascript 中的私有函数中调用实例变量
- 将 rails 实例变量用于 javascript 条件
- 将实例变量替换为局部变量
- 为什么当我通过 Ajax 请求刷新部分时我的实例变量为空
- 实例变量