在创建匿名对象期间如何访问类成员?
class foo {
private working = 'hello';
private notWorking = 'world';
public method() {
new DoSomethingClass(working, {
...,
this.notWorking //this throws an error as this has a different scope now
});
}
}
答案 0 :(得分:2)
传递配置对象时,请确保它使用正确的键:
new DoSomethingClass(working, {
theRequiredKey: this.notWorking
});
检查库中的键名。
您仍可以使用快捷方式,因为您使用与调用键相同的变量名,例如:
const theRequiredKey = this.notWorking;
new DoSomethingClass(working, {
theRequiredKey, // will create an object with key "theRequiredKey" and its value
});
这将根据需要工作。
还要检查您的图书馆是否有打字稿可用的类型,例如通过
npm install @types/{library-name}
或创建自己的类型。
答案 1 :(得分:0)
在运行时,无法保证const express = require('express');
const app = express();
const idxRouter = require('./router/index.js');
app.use('/',idxRouter);
具有正确的范围。可以在调用时使用const express = require('express'); //node express framework
const router = express.Router();
var fs = require('fs');
const db = require('../dbconfig/db');
const num = 2;
router.get('/',function(req,res){
function test(num) {
try {
db.query('SELECT a.explain from explain a where no =' + num, function (err, rows) {
if (err) {
console.log(err);
}
console.log(rows);
});
} catch (e) {
console.log(e);
}
};
test(num);
res.end(fs.readFileSync( '/Users/hoon/node/drawio/src/main/webapp/index.html'));
});//app.get '/' end
module.exports = router;
/ this
/ bind
自定义函数的作用域(专有术语为“上下文”),并且如果与对象分离,它将失去其原始上下文:< / p>
call
公共方法=()=> {...}
apply