2013-09-26 85 views
-1

基於對別人的代碼複製工程,我創建了下面的代碼(see fiddle here):JS變量和計算failling;

//INITIAL DATA: 
var geometry.id = "Norway"; 
var bounds = [[-5, 40], [10, 50]]; 

// START CALCULATIONS 
// WNES for West, North, East, South. 
// WNES borders' geo-coordinates (decimal degrees) 
var WNES = "", 
    WNES.item = geometry.id, 
    WNES.W = bounds[0][0], 
    WNES.N = bounds[1][1], 
    WNES.E = bounds[1][0], 
    WNES.S = bounds[0][1]; 
// Area's geo-dimensions (decimal degrees) 
var WNES.geo_width = (WNES.E - WNES.W), 
    WNES.geo_height = (WNES.N - WNES.S); 
// add a 5% padding on all WNES sides. 
var WNESplus.W = WNES.W - WNES.geo_width * 0.05, 
    WNESplus.N = WNES.N + WNES.geo_height * 0.05, 
    WNESplus.E = WNES.E + WNES.geo_width * 0.05, 
    WNESplus.S = WNES.S - WNES.geo_height * 0.05, 
    WNESplus.geo_width = (WNESplus.E - WNESplus.W), 
    WNESplus.geo_height = (WNESplus.N - WNESplus.S); 
// calcul center geo-coordinates 
var WNES.lat_center = (WNES.S + WNES.N)/2, 
    WNES.lon_center = (WNES.W + WNES.E)/2; 

//TEST: 
console.log("Test" + WNESplus.N + " and " + WNESplus.geo_width); 

這完全失效。看來我做了任務,並且分號使用錯誤。 我的錯誤是什麼,如何正確養生?

+1

err'var geometry.id =「Norway」;未捕獲的SyntaxError:意外的標記。 ' –

+0

我剛剛意識到你正在做的事情是錯的 - 你不能把'.'放在變量名中。你必須先定義一個對象。 – Adaz

+0

可能狼羣-1的問題至少可以解釋爲什麼,所以下次我不能做同樣的錯誤/錯誤行爲。 – Hugolpz

回答

1

您正在設置對象屬性而不創建它。變量聲明中不能使用點符號。

如下嘗試:

//INITIAL DATA: 
var bounds = [[-5, 40], [10, 50]]; 
var geometry = {}; 
geometry.id = "Norway"; 

// START CALCULATIONS 
// WNES borders' geo-coordinates (decimal degrees for West, North, East, South borders) 
var WNES = {}; 
WNES.item = geometry.id, 
WNES.W = bounds[0][0], 
WNES.N = bounds[1][1], 
WNES.E = bounds[1][0], 
WNES.S = bounds[0][1]; 
// Area's geo-dimensions (decimal degrees) 
WNES.geo_width = (WNES.E - WNES.W), 
WNES.geo_height = (WNES.N - WNES.S); 
// add a 5% padding on all WNES sides. 
var WNESplus = {}; 
WNESplus.W = WNES.W - WNES.geo_width * 0.05, 
WNESplus.N = WNES.N + WNES.geo_height * 0.05, 
WNESplus.E = WNES.E + WNES.geo_width * 0.05, 
WNESplus.S = WNES.S - WNES.geo_height * 0.05, 
WNESplus.geo_width = (WNESplus.E - WNESplus.W), 
WNESplus.geo_height = (WNESplus.N - WNESplus.S); 
// calcul center geo-coordinates 
WNES.lat_center = (WNES.S + WNES.N)/2, 
WNES.lon_center = (WNES.W + WNES.E)/2; 

console.log("Test"+ WNESplus.N +" and "+ WNESplus.geo_width); 

jsFiddle

+0

你的代碼在這裏工作。我看着它從中學習:) – Hugolpz

+0

好吧,很高興它爲你工作。 –

2

var用於聲明局部變量。使用它來「聲明」對象屬性是不正確的。

你應該只被做這樣的事情:

WNES.geo_width = (WNES.E - WNES.W); 
WNES.geo_height = (WNES.N - WNES.S); 

這就是說,你似乎是試圖屬性分配給一個字符串。這不起作用。您應該從下面開始:

var WNES = {}; 
1

您不能分配使用VAR對象(geometryWNESWNESplus)的性能。你必須先初始化它們作爲對象,然後分配屬性:

//INITIAL DATA: 
var bounds = [[-5, 40], [10, 50]]; 
var geometry = {}; 
geometry.id = "Norway"; 

// START CALCULATIONS 
// WNES borders' geo-coordinates (decimal degrees for West, North, East, South borders) 
var WNES = {}; 
WNES.item = geometry.id; 
WNES.W = bounds[0][0]; 
WNES.N = bounds[1][1]; 
WNES.E = bounds[1][0]; 
WNES.S = bounds[0][1]; 
// Area's geo-dimensions (decimal degrees) 
WNES.geo_width = (WNES.E - WNES.W); 
WNES.geo_height = (WNES.N - WNES.S); 
// add a 5% padding on all WNES sides. 
var WNESplus = {}; 
WNESplus.W = WNES.W - WNES.geo_width * 0.05; 
WNESplus.N = WNES.N + WNES.geo_height * 0.05; 
WNESplus.E = WNES.E + WNES.geo_width * 0.05; 
WNESplus.S = WNES.S - WNES.geo_height * 0.05; 
WNESplus.geo_width = (WNESplus.E - WNESplus.W); 
WNESplus.geo_height = (WNESplus.N - WNESplus.S); 
// calcul center geo-coordinates 
WNES.lat_center = (WNES.S + WNES.N)/2; 
WNES.lon_center = (WNES.W + WNES.E)/2; 

console.log("Test " + WNESplus.N + " and " + WNESplus.geo_width); 

這裏是workign jsFiddle

1

你不允許一個變量聲明爲這種格式的對象。包含點符號或對象的所有東西都應聲明爲var something = {}。舉個例子:

這個geometry.id = "Norway";會100%失敗。但是您可以聲明它爲:

var geometry = { id: "Norway"};並遵循其他人的相同模板。

+0

感謝您的解釋清楚〜 – Hugolpz

+0

我希望我能說得更清楚。但是,在你的老闆眼中是不容易的。我希望它有幫助,祝你好運:) –