2016-03-23 61 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