2012-12-04 65 views
6

爲什麼這不起作用?如何在JavaScript中創建一個新的對象?

var sheep = function(options){ 
     this.options = {sizes: 100, 
         eat: 100, 
       colors: 'white', 
       running: function() { 
        return this.sizes + this.eat; 
       } 
      } 
    }; 

    var blacksheep = new sheep({colors:'black'});  

    alert('blackcsheep color is ' + blacksheep.colors);//error undefined 
    alert('blackcsheep color is ' + blacksheep.options.colors);// it return white 
    alert('blackcsheep running is ' + blacksheep.running());//error 
+1

'sheep'已經是一個對象。我認爲閱讀JavaScript基礎知識將對您最有幫助:[MDN JavaScript指南](https://developer.mozilla.org/en-US/docs/JavaScript/Guide),特別是[使用對象](https:// developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects)。 –

+0

@FatDogMark:你想要什麼?羊已經是一個對象。 – karthick

+0

但我如何在綿羊上製造另一隻黑羊基地?繼承綿羊的屬性 – FatDogMark

回答

3

的語法:

var sheep = {sizes:100, eat:100, colors:'white',running:function(){ 
     return this.sizes+this.eat; 
     } 
    }; 

是一個對象文字。它定義了一個對象的實例,但不定義它的類。因此,無法「新建」另一個對象實例。

看看jQuery的extend功能:

var blacksheep = { 
} 

$.extend(blacksheep, sheep, { color: black }); 

這會的sheep所有屬性複製到blacksheep,那麼第三個參數合併爲blacksheep,有效地實現你想要什麼。

+0

$ .extend對我來說只是方便,好吧我接受答案 – FatDogMark

1

要再拍害羣之馬基於羊,在這種情況下,你可以(使用jQuery)做:

var blacksheep = $.extend(sheep, { color: 'black' }); 
+1

而沒有jQuery? –

+0

我不會這樣做沒有jQuery。克隆對象很麻煩 - 應該使用函數Sheep(){...}。參考http://stackoverflow.com/questions/728360/copying-an-object-in-javascript克隆javascript對象。 –

1

您可以創建這樣一個羊對象。

function Sheep(sizes,eat,colors){ 
    this.sizes = sizes; 
    this.eat = eat; 
    this.colors = colors; 
    this.running = function(){ 
    return this.sizes+this.eat; 
    } 

    } 

或者,也可以寫這樣也

function Sheep(sizes,eat,colors){ 
    this.sizes = sizes; 
    this.eat = eat; 
    this.colors = colors;   
    } 
sheep.prototype.running = function(){ 
return this.sizes + this.eat;  
} 

變種sheep1 =新綿羊( '100', '100', '白色');

1
var sheep = function(){ 
    this.sizes = 100; 
    this.eat = 100; 
    this.colors = 'white'; 
    this.running = function(){ 
     return this.sizers + this.eat; 
    } 
} 
+0

爲什麼我會在所有變量中都需要'this'...我不能將我的羊對象包裝在一個函數中並將它變成類? – FatDogMark

+0

var a = new sheep();現在有財產大小......因爲'這' – karaxuna

+0

'這'是目前的羊類。請閱讀這裏是什麼意思:http://www.quirksmode.org/js/this.html – karaxuna

-1

Javascript是基於原型而不是基於類。它不使用類,也是面向對象的。

var whitesheep =new sheep("100","100","white","running"); 
var blacksheep =new sheep("100","100","black","running"); 
+1

您是否在瀏覽器控制檯中嘗試了這一點?這不會工作,除非你定義一個函數對象並返回某些對象的屬性作爲值傳遞。 –

1

您不像使用強類型語言一樣在JavaScript中聲明對象。通過使用像這樣的函數聲明對象:

function sheep() { 
    this.color = "white"; 
    this.size = 200; 
    this.speed = 100; 
    this.running = function() { 
     return "the sheep is running!"; 
    }; 
} 

var blacksheep = new sheep(); 

alert('sheep size is ' + blacksheep.size); 
alert('sheep running is ' + blacksheep.running());​ 

,因爲你正在創建一個名爲options子對象的新對象新對象不起作用。 options包含您的所有方法。因此只有你給會給你正確的反應這三條線的第二個:

alert('blackcsheep color is ' + blacksheep.colors); 
alert('blackcsheep color is ' + blacksheep.options.colors); // Only this one correctly references `options`. 
alert('blackcsheep running is ' + blacksheep.running()); 
+0

我看到有些人讓課堂不需要這個放置this.options {...}的所有東西,然後他可以創建像blacksheep = new sheep({colors:'black',sizes = 10})這樣的新對象; , 怎麼做?? – FatDogMark

+0

我的第一個例子顯示瞭如何做到這一點。當您調用函數'new sheep()'時,'this'引用正在創建的新對象。什麼'var blacksheep = new sheep()'將'sheep()'函數中'this'引用的所有屬性應用於變量'blacksheep'。因此,您可以像我一樣繼續引用,'blacksheep.size','blacksheep.running()'等等。試試這裏:http://jsfiddle.net/SZQVn/ –

0

你的情況羊已經是一個對象,你不能創建一個對象的對象。 您可以直接使用該對象的屬性。

但我想你想是這樣的

VAR羊= {尺寸:100,吃飯:100,顏色: '白色',在運行:函數(){ 回報this.sizes +這一點。吃; } }; Object.defineProperty(sheep,'colors',{value:'black' ,writable:true});

由於

相關問題