Javascript 在typescript中访问类属性
我有一个typescript类,其构建如下Javascript 在typescript中访问类属性,javascript,node.js,typescript,Javascript,Node.js,Typescript,我有一个typescript类,其构建如下 export default class myClass{ myns: any; async initializing() { … this.myns = na.map(function (item) { return item["name"]; }); // here we have value console.log(this.myns); } search(test, input) { input = input || "";
export default class myClass{
myns: any;
async initializing() {
…
this.myns = na.map(function (item) {
return item["name"];
});
// here we have value
console.log(this.myns);
}
search(test, input) {
input = input || "";
return new Promise(function (resolve) {
let fuz= fuzzy.filter(input, this.myns); //here I want to access myns but here it comes undefined in debug
resolve(
fuz.map(function (el) {
return el.original;
})
);
});
}
}
我想访问函数search
中的myns
(在函数中search是非加密的,但在init中它有数据)search我怎么做
不仅myns
未定义此
也未定义尝试执行(resolve)=>{
而不是函数(resolve){
,以便将其绑定到回调
编辑:
运行此代码对我很有用:
class myClass {
myns: any;
async initializing() {
this.myns = [{ name: 'test1' }, { name: 'test2' }].map(function (item) {
return item["name"];
});
console.log(this.myns);
}
search(test, input) {
input = input || "";
return new Promise((resolve) => {
console.log('test');
console.log(this.myns);
resolve();
});
}
}
const test = new myClass();
test.initializing();
test.search('lala', 'lala2');
正如预期的那样,产出是:
[ 'test1', 'test2' ]
test
[ 'test1', 'test2' ]
你正在使用的模糊库是什么