2011-01-19 46 views
0

在我的代碼中是否有任何錯誤esp var newpoint [0] = new Point;。我wnat知道如何做空中接力在JavaScript面向對象的JavaScript

function Point() 
{ 
    var x; 
    var y; 
} 

var length = 1; 
var arrayindex; 
var newpoint[0] = new Point; 
newpoint[0].x = 10; 
newpoint[0].y = 10; 
for(i=0 ; i<10; i ++) 
{ 
    newpoint[length].x = 10*i; 
    newpoint[length++].y = 10*i; 
} 

for(arrayindex in newpoint) 
{ 
    alert('x='+newpoint[arrayindex].x +'y='+newpoint[arrayindex].y); 
} 

編輯:感謝所有。我想出了兩個代碼,需要知道哪一個更適合任何sugesstion和protips。這兩個工作

function Point() 
{ 
    this.x; 
    this. y; 
} 

var length = 0; 
var newpoint = []; 

for(i=0 ; i<10; i ++) 
{ 
    newpoint[length] =new Point(); 
    newpoint[length].x = 10*i; 
    newpoint[length++].y = 10*i; 
} 

for(arrayindex in newpoint) 
{ 
    alert('x='+newpoint[arrayindex].x +'y='+newpoint[arrayindex].y) 
} 

var length = 0; 
var newpoint = []; 
for(i=0 ; i<10; i ++) 
{ 
    newpoint[length] = {}; 
    newpoint[length].x = 10*i; 
    newpoint[length++].y = 10*i; 
} 
for(var arrayindex in newpoint) 
{ 
    alert('x='+newpoint[arrayindex].x +'y='+newpoint[arrayindex].y) 
} 
+1

你仍然在構造函數中使用var,它不會工作,你必須使用this.x,因爲我在我的帖子下面顯示。它工作的原因是因爲當你在代碼中稍後分配它們時創建x和Y屬性,但是如果將來添加默認值,它將不起作用。 – 2011-01-19 12:50:16

回答

2

你試圖使用X和Y屬性? 你不需要在函數中聲明它們。你可以直接在javascript對象上使用它們。

var newpoint = []; // Declares newpoint as array 
var length = 1; 
for(var i=0 ; i<10; i ++) 
{ 
    newpoint[length] = {}; //Declares a new object. 
    newpoint[length].x = 10*i; 
    newpoint[length++].y = 10*i; 
} 

所述.X和.Y爲對象

+0

感謝它的工作 – aWebDeveloper 2011-01-19 12:22:40

0

newpoint必須是一個陣列的第一屬性是自動關聯,則不能使用:

var newpoint[0] = new Point(); //newpoint has to be declared on its own to be an array if you like to use [0] indexing; 

使用

var newpoint = []; 
newpoint[0] = new Point(); 

var newpoint = [new Point()]; //Cipi's suggestion for oneliner 

您需要()後面的類型。 new關鍵字調用可以接受參數的構造方法,所以你需要使用()。

var newpoint[0] = new Point(); 

而且該類ddeclaration是不正確的 聲明是你的構造和必須返回它的自我:

function Point() 
{ 
    this.x = null; //Or whatever default value you like 
    this.y = null; 
    /* 
    "This" makes the x and y variables members of the object instead of 
    local variables if the constructor function, without "this" they will 
    be forgotten as soon as the function ends. */ 
} 

您可以添加無論是在構造方法

function Point() 
{ 
    this.x = null; //Or whatever default value you like 
    this.y = null; 
    this.list = function() 
    { 
     alert(this.x); 
    }; 
} 

或使用原型

function Point() 
{ 
    this.x = null; //Or whatever default value you like 
    this.y = null; 
}  
Point.prototype.list = function() 
{ 
    alert(this.x); 
} 
+0

構造函數不必返回`this`,語言本身需要照顧那個。看到https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript除此之外,你似乎是誰糾正的最重要問題的任擇議定書只有一個 - `本` – davin 2011-01-19 12:21:40

+0

ü可以編輯並解釋你的代碼有點 – aWebDeveloper 2011-01-19 12:25:03

5

之前我的評論你的代碼,閱讀教程:https://developer.mozilla.org/en/JavaScript/Guide

現在的娛樂休閒:

function Point() 
{ 
    var x; // great a local variable! 
    var y; // another one! they drop out of scope... (protip: use 'this') 
} 

var length = 1; // ??? 
var arrayindex; // better declare that in the for 
var newpoint[0] = new Point; // that works yes, but only the `new Point` part, 'newpoint[0]' what is that supposed to do? 
newpoint[0].x = 10; // there's no x property on a `Point` here 
newpoint[0].y = 10; 
for(i=0 ; i<10; i ++) // i leaks into global scope 
{ 
    newpoint[length].x = 10*i; // ??? Fails, newpoint is not an array 
    newpoint[length++].y = 10*i; // Again, what's the point of this? 
} 

for(arrayindex in newpoint) // great! a extremely slow for in to iterate over an array.. 
// pro tip, this will enumerate over all the _properties_ of the _object_ 
{ 
    // usual complain about the missing 'hasOwnProperty' call (TM) 
    alert('x='+newpoint[arrayindex].x +'y='+newpoint[arrayindex].y); 
} 
2

請參閱VO韋策爾的回答

你可能會尋找類似這樣

一些事情
// OOP style template obj 
Point = { 
    x:null, 
    y:null 
}; 

var newpoint = new Array(); // An array to hold Point type objects 

// now add some entriedsto the above array 
for(var i=0 ; i<10; i ++) 
{ 
    newpoint[i] = new Point(); // create a new point object 
    newpoint[i].x = 10*i; // set the values of the point object you just created 
    newpoint[i].y = 10*i; 
} 

for(var arrayindex in newpoint) 
{ 
    alert('x='+newpoint[arrayindex].x +', y='+newpoint[arrayindex].y); 
}​ 
0

我認爲你想這樣做:var newpoint = [new Point]。 (你的方法是用新對象初始化元素0,但新點的元素0不存在,因爲你從未說新點是一個數組。)這樣你將數組newpoint的第一個元素初始化爲新的點,所以這個:newpoint[0].x = 10可以執行。

但是這樣:newpoint[length].x = 10*i將會失敗,因爲你的數組只有一個元素被初始化,並且它是newpoint [0],其他的都是undefined。我想你想先初始化它們(所有10個),像這樣的例子:

for(var i=1; i<10; i++) 
{ 
    newpoint.push(new Point()); 
} 

而且,變化點像下面,如果你想能夠看到x和y在你創建的對象!

function Point() 
{ 
    this.x; 
    this.y; 
} 

那麼你的代碼將運行良好......

2

您可以使用一個構造函數,以便而不必鍵入很多東西創建點。當使用new關鍵字調用時,此函數中的this關鍵字指的是正在創建的對象。

該構造函數以xy作爲參數並將其放入對象中。

function Point(x, y) { 
    this.x = x; 
    this.y = y; 
} 

然後你想要一個點數組。在使用之前,您需要先創建它。

var newpoint = []; 

添加新點很容易。在函數構造函數的幫助下,它可能不夠簡潔。

for (var i = 0; i < 10; i ++) { 
    newpoint.push (new Point(10 * i, 10 * i)); 
} 
  • new Point(10 * i, 10 * i)創建一個具有指定xy座標點的新實例。
  • newpoint.push用於將傳遞的參數添加到數組中。所以沒有更多的length跟蹤!
  • 結合起來,這會創建一個新點,並將其添加到數組中。

然後顯示它,你可以像以前一樣迭代數組。請注意,這次我使用newpoint.length來表示數組的長度。

如果可以你上面的數字10不必進行任何修改這個循環中,因爲我們得到動態的長度。

for (var i = 0; i < newpoint.length; i ++) { 
    alert('x=' + newpoint[i].x + ' y=' + newpoint[i].y); 
}