2011-10-18 90 views
13

我有下面的JavaScript代碼。在Chrome,Firefox,Android模擬器,三星Galaxy S上的Firefox(薑餅2.3.3)以及iPod上的Safari瀏覽器都能正常工作。在三星Galaxy S上的本機瀏覽器上沒有。創建一個對象兩次會產生不同的結果

該代碼創建一個對象並測試該對象上的值。它第一次創建的對象是正確的。第二次創建對象的值不正確。

這是一個錯誤的JavaScript或V8或設備?你將如何去解決它?

var Padding = function(pleft, ptop, pright, pbottom) { 
    this.top = 20; 
    this.left = 1; 
    this.right = 0; 
    this.bottom = 0; 
    this.left = pleft; 
    this.top = ptop; 
    this.right = pright; 
    this.bottom = pbottom; 
}; 

function testPadding() { 
    var p; 
    p = new Padding(91, 92, 93, 94); 
    alert(p.left.toString() + "," + p.top.toString() + "," + p.right.toString() + "," + p.bottom.toString()); 
} 

testPadding(); // 91,92,93,94 - correct 
testPadding(); // 1,20,93,0 - should be 91,92,93,94 
testPadding(); // 1,20,93,0 - should be 91,92,93,94 

編輯:我發現它爲什麼在模擬器中工作。模擬器使用不同的JavaScript引擎。它使用JSC而不是V8。在http://code.google.com/p/android/issues/detail?id=12987中有一小段代碼可以幫助你計算出它使用的引擎。仿真器使用JSC,三星Galaxy S使用V8。

+0

這是ECMAScript實現中的一個錯誤,無論它叫什麼。 :-) – RobG

+0

這絕對是一個錯誤,但誰?三星? V8?我正在給三星發送電子郵件,但是誰知道他們是否會將其轉發給合適的人或者忽略它。 –

+0

我會把它與三星文件。尋找其他瀏覽器使用相同的腳本引擎,看看他們是否有相同的錯誤 - 如果你找到它,請向開發V8(Google?)的人提出錯誤。如果他們已經在更高版本中修復了這個問題,那麼三星會將他們的瀏覽器更新到更高版本。 – RobG

回答

1

由於V8引擎如何進行垃圾回收和緩存,我想它在開始返回結果之前沒有用該對象完成。你有沒有嘗試將你的代碼改爲以下內容?它是否每次都使用此代碼返回預期結果?

var Padding = function(pleft, ptop, pright, pbottom) { 
    this.top = (ptop != null) ? ptop : 20; 
    this.left = (pleft!= null) ? pleft: 1; 
    this.right = (pright!= null) ? pright: 0; 
    this.bottom = (pbottom!= null) ? pbottom: 0; 
}; 

function testPadding() { 
    var p; 
    p = new Padding(91, 92, 93, 94); 
    alert(p.left.toString() + "," + p.top.toString() + "," + p.right.toString() + "," + p.bottom.toString()); 
} 

testPadding(); // ? 
testPadding(); // ? 
testPadding(); // ? 
相關問題