gpt4 book ai didi

javascript - 出现错误 "property does not exist"

转载 作者:行者123 更新时间:2023-12-03 03:47:43 24 4
gpt4 key购买 nike

我修改了我的问题,使其更加具体。现在我不关心所需的行为,我只需要纠正语法错误

我正在学习this tutorial我在这段代码中遇到一个错误。

严重性:“错误”

消息:“类型‘PagerserviceProvider’上不存在属性‘offset’。”

实际上我对这三个变量有相同的错误。

that.pageSize,that.offset,that.size

public async getPager(tableName:string,pageSize: number = 10) {
let pageSize = pageSize;
let offset = 0;
let limit = pageSize;
let size = await this.getTotal(tableName);
let that = this;
return {
initialPage:function(){

return new Promise((resolve,reject)=>{
var d = [];
that.executeSql(tableName,limit,offset).then((data)=>{
console.log(JSON.stringify(data));
for(var i = 0 ; i < data.rows.length ; i++)
{
d.push(data.rows.item(i));
}
resolve(d);
},(e)=>{
reject(e);
});
});

},
nextPage:function(){
if(that.offset <= that.size - that.pageSize )
{
that.offset += that.pageSize;
}
return new Promise((resolve,reject)=>{
var d = [];
that.executeSql(tableName,limit,offset).then((data)=>{
for(var i = 0 ; i < data.rows.length ; i++)
{
d.push(data.rows.item(i));
}
resolve(d);
},(e)=>{
reject(e);
});
});
}
};}

最佳答案

当使用关键字function声明函数时,函数的this并不引用上面的this。因此在函数体中使用 this 指的是函数本身。

您面临的问题与您的函数是在已经定义了 this 的类中声明的事实有关,因此您需要一种方法来引用上面的 this code> 在嵌套函数内部时。

class Test {

hello () { console.log('hello') }

method () {
this.hello() // It will work because `this` refers to the class
function sayHello () {
return this.hello()
// it won't work because `this` refers to the function sayHello
}
return sayHello()
}
}

要绕过此限制,您可以在代码位于上部作用域时将上部 this 保存在变量中。该变量通常称为 thatself

class Test {

hello () { console.log('hello') }

method () {
var that = this // that is now refering to the class
this.hello() // It will work because `this` refers to the class
function sayHello () {
return that.hello()
// that is still refering to the class so it will succeed
}
return sayHello()
}
}

编辑:

避免使用 that 的另一个技巧是使用 ES6 箭头函数。在箭头函数内,this 始终指的是上层作用域。

class Test {

hello () { console.log('hello') }

method () {
this.hello() // It will work because `this` refers to the class
// `this` refers to the upper scope by default so it works
const sayHello = () => this.hello()
return sayHello()
}
}

编辑2:

您的代码应该是:

  public async getPager(tableName: string, pageSize: number = 10) {
let pageSize = pageSize;
let offset = 0;
let limit = pageSize;
let size = await this.getTotal(tableName);
let that = this;
return {
initialPage: function () {

return new Promise((resolve,reject)=>{
var d = [];
that.executeSql(tableName, limit, offset).then(data => {
console.log(JSON.stringify(data));
for(var i = 0 ; i < data.rows.length ; i++) {
d.push(data.rows.item(i));
}
resolve(d);
}, e => {
reject(e);
});
});

},
nextPage: function () {
if(offset <= size - pageSize ) {
offset += pageSize;
// no need to use `that` because you used `let`
}
return new Promise((resolve, reject) => {
var d = [];
that.executeSql(tableName, limit, offset).then(data => {
for(var i = 0 ; i < data.rows.length ; i++) {
d.push(data.rows.item(i));
}
resolve(d);
}, e => {
reject(e);
});
});
}
};
}

关于javascript - 出现错误 "property does not exist",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45302627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com