2016-03-23 62 views
1

注意:是的,有類似的問題,但我無法將集合傳遞給函數。將對象添加到擴展數組的JS集合(繼承?)

$j = jQuery.noConflict(); 

// objects.json shows: {"objects": ["hello", "hi", "yo"]} 

function ObjectCollection() { 

} 

ObjectCollection.prototype = []; 


ObjectCollection.prototype.fetch = function() { 
var parent = this; 
$j.getJSON("objects.json", function(data) { 
    $j.each(data.objects, function(i, customObject) { 
     var myCustomObject = new CustomObject(customObject); 
     parent.push(myCustomObject); 
    }); 

    console.log(parent); // array exists 

}); 
     console.log(parent); // its gone! 

}; 


function CustomObject(name) { 
    this.name = name; 
} 

function doSomethingWithCollection(objectCollection) { 
    this.objectCollection = objectCollection; 
    this.objectCollection.fetch(); 
    console.log(this.objectCollection); // shows object collection with object in console 
    console.log(this.objectCollection.length); // equals 0?? should be 3! 
    this.objectCollection.forEach(function(object) { 
     // it wont iterate 
     console.log(object); 


    }); 
} 
var collection = new ObjectCollection; 
// if i uncomment the next two lines, the code will work 
// var object = new CustomObject('myName'); 
// collection.push(object); 
doSomethingWithCollection(collection); 

編輯...好吧,我的問題是這樣的:https://jsfiddle.net/qd42fknL/

請不要建議插件。我想創建我自己的對象收集器。

我的代碼是怎麼回事?

我做了一個小提琴...

如果我開始收集與function..it將工作以外的對象,所以這是一個繼承的問題。這是怎麼回事?

+0

它是否給你一個錯誤的小提琴? – amanuel2

+0

你有一些例子嗎? –

+0

你不能像這樣擴展數組。 [你不能在ES5中擴展'Array'](http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/)。 – Bergi

回答

0

編輯

我意識到這個問題是不是繼承的。問題在於你發送了一個ajax請求,在它實際完成之前,你正在做console.log。所以,這會產生錯誤的結果。

有兩種解決方案可以解決這個問題。

  1. 使用同步AJAX請求(但不推薦)
  2. 等待異步AJAX請求第一結束。

這是一個使用異步ajax調用的工作解決方案。

$j = jQuery.noConflict(); 

function ObjectCollection() { } 
ObjectCollection.prototype = []; 

ObjectCollection.prototype.fetch = function(callback) { 
    var parent = this; 
    $j.getJSON("objects.json", function(data) { 
     $j.each(data.objects, function(i, customObject) { 
      var myCustomObject = new CustomObject(customObject); 
      parent.push(myCustomObject); 
     }); 

     callback(); // notify request is completed 
    }); 
}; 

function CustomObject(name) { 
    this.name = name; 
} 

function doSomethingWithCollection(objectCollection) { 
    this.objectCollection = objectCollection; 
    var that = this; 

    this.objectCollection.fetch(function() { 

     // do this only after ajax finishes 
     console.log(that.objectCollection); 
     console.log(that.objectCollection.length); 
     that.objectCollection.forEach(function(object) { 
      console.log(object); 
     }); 
    }); 
} 

var collection = new ObjectCollection; 
doSomethingWithCollection(collection); 

這裏是https://jsfiddle.net/qd42fknL/2/

+0

這對其他類非常適用,但不適用於'Array'。 'Array.apply(this,arguments)'什麼也不做,'.length'不起作用。 – Bergi

+0

@Bergi coll.length工作正常。請看看這裏https://jsfiddle.net/2j0ef55n/ –

+0

[不是真的](https://jsfiddle.net/2j0ef55n/1/)。它只有預期的值,因爲你使用了'push',但它不像你期望的那樣工作。 – Bergi