在纯JavaScript中从构造函数推送到数组

时间:2017-03-17 20:46:18

标签: javascript arrays object constructor push

我正在尝试从构造函数中构建一个对象数组。我不知道它是否可行或可取。但是我正在努力为实践建立这个,我想知道它为什么不起作用。

// Variables.
const VVD = new Party("VVD", 33);

// List of objects.
var theList = [];

// Party constructor.
function Party(name, seats) {
	this.name = name;
	this.seats = seats;
	//theList.push(this); // This isn't working.
	this.pushToTheList = function() { 
		theList.push(this);
	}
	this.pushToTheList(); // And neither is this.
}

这是我收到的错误:Uncaught TypeError: Cannot read property 'push' of undefined即使我将this替换为"test",我仍然会收到同样的错误。

在构造函数之外,这很好用: theList.push(VVD);

为什么这不起作用?是否有更好,更智能的方法将对象推送到数组?

指向CodePen的链接:http://codepen.io/MichaelVanDenBerg/pen/gmXZej

2 个答案:

答案 0 :(得分:2)

在创建theList数组之前,您的Party构造函数被称为

函数声明(如theList = []构造函数)被提升到其作用域的顶部;但是,var theList之类的变量的赋值不是(即使var theList; // Variables. const VVD = new Party("VVD", 33); // List of objects. theList = []; 声明本身已被提升)。因此,您的代码将被解释为:

theList

在第一次调用构造函数时,您可以更清楚地看到undefinedtheList的原因。尝试重新排序语句,以便在VVD之前创建// List of objects. var theList = []; // Variables. const VVD = new Party("VVD", 33); // Party constructor. function Party(name, seats) { this.name = name; this.seats = seats; //theList.push(this); // This works this.pushToTheList = function() { theList.push(this); } this.pushToTheList(); // And so does this. } console.log(theList)



echo "$(</dev/stdin)" > /tmp/file
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您在定义$(function() { $.widget("custom.altTabs", { _create: function() { this.wrapper = $("<div>") .addClass("ui-altTabs custom-altTabs ui-corner-all ui-widget ui-widget-content") .insertAfter(this.element); this.element.hide(); this._createTabs(); }, _createTabs: function() { this.list = $("<ul>") .appendTo(this.wrapper) .addClass("ui-altTabs-nav ui-corner-all ui-helper-reset ui-helper-clearfix ui-widget-header") .attr("role", "tablist"); this.content = []; var origSelf = this.element; var contentList = origSelf.find("ul li"); var contentDivs = origSelf.find("div"); var currentUiCount = $(document).find("[id^='ui-id-']").length; var self = this.list; contentDivs.each(function(ind, el) { var label = contentList.eq(ind).find("a").text(); var target = $(el).attr("id"); var item = $("<li>", { class: "ui-altTabs-tab ui-corner-top ui-state-default ui-tab", role: "tab" }).appendTo(self); $("<span>", { id: "ui-id-" + ++currentUiCount, "data-rel": target, class: "ui-altTabs-anchor", role: "presentation", }).html(label).click(function(e) { self.find(".ui-tabs-active").removeClass("ui-tabs-active") .removeClass("ui-state-active"); $(e.target).parent().addClass("ui-tabs-active ui-state-active"); contentDivs.hide(); $("#" + $(e.target).data("rel")).show(); }).appendTo(item); console.log("Appending Item to list: ", item); }); self.find("li:eq(0)").addClass("ui-tabs-active ui-state-active"); for (i = contentDivs.length; i--; i >= 0) { var panel = contentDivs.eq(i); panel.addClass("ui-tabs-panel ui-corner-bottom ui-widget-content") .attr("role", "tabpanel") .insertAfter(self) .hide(); if (i == 0) { panel.show(); } } }, }); $("#tabs").altTabs(); });

之前创建了新的Party
theList