2017-07-25 67 views
0

我修改了我的問題以更具體。現在我不在乎期望的行爲,我只需要糾正語法錯誤獲取錯誤「property does not exist」

我正在學習this tutorial我在這段代碼中遇到一個錯誤。

嚴重性:偏移PagerserviceProvider '上類型不存在' '屬性': '錯誤'

消息'。

實際上,我對這三個變量有同樣的錯誤。

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); 
       }); 
      });      
     }    
    };} 
+1

有什麼問題到底是什麼? –

+1

[var that = this是什麼意思?]在JavaScript中是什麼意思?(https://stackoverflow.com/questions/4886632/what-does-var-that-this-mean-in-javascript) –

+0

@Jeremy Thille我有一個錯誤在我的變量不承認,我猜其原因是選擇正確的情況下 – david

回答

1

當您使用關鍵字function來聲明一個函數時,函數的這個並不是指上面的this。所以在函數體中使用this,就是指函數本身。

您面臨的問題與您的函數在已定義this的類中聲明的事實有關,因此您需要一種方法在嵌套函數內引用上層this

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); 
       }); 
      });      
     }    
     }; 
    } 
+0

我一直都知道,但變量爲什麼我的代碼有錯誤,我用同樣的規則,你說 – david

+0

你有明確聲明'that.offset = ...,that.pageSize = ...'? 'let'不會在默認情況下將它添加到'that'中 –

+0

我使用了「var」,但仍有錯誤 – david

0

「那個」是用來存儲的原始值的變量只是名字「這個」在開始你的代碼,因爲'this'的值發生了變化。變量名稱可能很容易成爲「狗」或「蘋果」。

如果您打算在該時間點訪問「this」的當前值,您可以選擇稍後在代碼中使用'this'。否則,你可能會引用你原來存儲它的值的​​變量,例如'那','狗'或'蘋果'。

+0

我無法理解的是,我定義相同的上下文背景下,我定義「即」使「that.offset」是沒有意義的 – david

0

getPager是一個方法:如果你有一個已經失去了上下文調用它,that將獲得當前this值,沒有指出正確的上下文。

const someInstance = new SomeClass() 

someInstance.getPager() // You call getPager directly from the someInstance context 

someHTMLButton.onClick = someInstance.getPager // Here the getPager method lost its context 

一種解決方案是結合getPagersomeInstance。通過這種方式,它始終將其上下文this指向someInstance

someHTMLButton.onClick = someInstance.getPager.bind(someInstance) 
相關問題