如何在Javascript中调用构造函数外的属性值?

如何在Javascript中调用构造函数外的属性值?,javascript,Javascript,编辑:为了清晰起见,我加入了我的更多代码 我已经使用构造函数创建了一个表来创建多个表体,但是我不想重复页眉和页脚,所以我分别创建了它们。但是,创建的属性之一是填充的空数组。我目前无法访问它 function Store(storeId, minCustomer, maxCustomer, avgSale) { this.storeId = storeId; this.minCustomer = minCustomer; this.maxCustomer = maxCust

编辑:为了清晰起见,我加入了我的更多代码 我已经使用构造函数创建了一个表来创建多个表体,但是我不想重复页眉和页脚,所以我分别创建了它们。但是,创建的属性之一是填充的空数组。我目前无法访问它

function Store(storeId, minCustomer, maxCustomer, avgSale) {
    this.storeId = storeId;
    this.minCustomer = minCustomer;
    this.maxCustomer = maxCustomer;
    this.avgSale = avgSale;
    this.arrCookie = [];
    this.addDom();
};
Store.prototype.addDom = function() {
    this.cookiesPerHour();
    var findTable = document.getElementById('table');
    var tBody = document.createElement('tbody');
    var trtBody = document.createElement('tr');
    var tdstoreId = document.createElement('td');
    tdstoreId.innerHTML = this.storeId;
    findTable.appendChild(tBody);
    tBody.appendChild(trtBody);
    trtBody.appendChild(tdstoreId);
    var totalStoreCookies = 0;
    for (var i = 0; i < this.arrCookie.length; i++) {
        var info1 = document.createElement('td');
        info1.innerHTML = this.arrCookie[i];
        trtBody.appendChild(info1);
    };
    for (var i = 0; i < this.arrCookie.length; i++) {
        totalStoreCookies += this.arrCookie[i];
    };
    var info2 = document.createElement('td');
    info2.innerHTML = totalStoreCookies;
    trtBody.appendChild(info2);
};

function makeTHF() {
    var totalCookies = 0;
    var findTable = document.getElementById('table');
    var tHead = document.createElement('thead');
    var trtHead = document.createElement('tr');
    var findTable = document.getElementById('table');
    var tFoot = document.createElement('tfoot');
    var trtFoot = document.createElement('tr');
    var hourlyTotal = document.createElement('td');
    findTable.appendChild(tHead);
    tHead.appendChild(trtHead);
    findTable.appendChild(tFoot);
    tFoot.appendChild(trtFoot);
    for (var i = 0; i < 17; i++) {
        var time = document.createElement('th');
        time.innerHTML = arrTime[i];
        trtHead.appendChild(time);
    };
    for (var i = 0; i < arrCookie.length; i++) {
        var storeName = [PDXAirport, PioneerSquare, Powells, StJohns, Waterfront];
        hourlyTotal.innerHTML = 'Total';
        var colTotal = 0;
        for (var j = 0; j < storeName.length; j++) {
            colTotal += storeName[j].arrCookie[i];
            hourlyTotal.innerHTML = colTotal;
        };
        trtFoot.appendChild(hourlyTotal);
        console.log(storeName);
    };
};
var PDXAirport = new Store('PDX Airport', 23, 65, 6.3);
var PioneerSquare = new Store('Pioneer Square', 3, 24, 1.2);
var Powells = new Store('Powell\'s', 11, 38, 3.7);
var StJohns = new Store('St. John\'s', 20, 38, 2.3);
var Waterfront = new Store('Waterfront', 2, 16, 4.6);
makeTHF();
函数存储(storeId、minCustomer、maxCustomer、avgSale){
this.storeId=storeId;
this.minCustomer=minCustomer;
this.maxCustomer=maxCustomer;
this.avgSale=avgSale;
this.arrCookie=[];
this.addDom();
};
Store.prototype.addDom=函数(){
这是每小时烹饪次数();
var findTable=document.getElementById('table');
var tBody=document.createElement('tBody');
var trtBody=document.createElement('tr');
var tdstoreId=document.createElement('td');
tdstoreId.innerHTML=this.storeId;
findTable.appendChild(tBody);
tBody.appendChild(trtBody);
trtBody.appendChild(tdstoreId);
var totalStoreCookies=0;
for(var i=0;i

当我在makeTHF函数中尝试使用arrCookie时,当我尝试使用arrCookie.length作为“for”循环的指令时,我会收到一个错误,说“未定义”。在它不改变结果之前放置“这个”。我想不出为什么这不起作用。在原型功能期间正确填充阵列。为了简洁起见,我已经删除了一些与问题无关的代码。

您可以使用访问器方法

var theStore = new Store()
theStore.getArrCookie();

其中
getArrCookie
返回
this.arrCookie

您可以直接调用属性
store.arrCookie
或创建一个原型函数作为getter:

Store.prototype.getCookies = function() {
  return this.arrCookie;
};
下面是一个工作示例:

function Store(storeId, minCustomer, maxCustomer, avgSale) {
    this.storeId = storeId;
    this.minCustomer = minCustomer;
    this.maxCustomer = maxCustomer;
    this.avgSale = avgSale;
    this.arrCookie = [];
    this.addDom();
};

Store.prototype.addDom = function() {};

Store.prototype.addCookie = function(cookie) {
  this.arrCookie.push(cookie);
};

Store.prototype.getCookies = function() {
  return this.arrCookie;
};

var store = new Store(1, 0, 1, 10);
store.addCookie('cookie1');
store.addCookie('cookie2');
console.log(store.arrCookie);
console.log(store.getCookies());

您可以在上运行此代码。

如果您执行类似于
var s=newstore(…)
,然后可以通过
s.arrCookie
访问
arrCookie
属性。如果您还将函数转换为在最后返回
this
,您还可以通过
var s=Store(…)访问
arrCookie

如果使用
Store.prototype.arrCookie='abc'
,实际上没有效果,因为它将被this.arrCookie=[]
覆盖;在作为构造函数的函数体内部,但如果通过原型添加属性,则可以在新对象中访问该属性(例如,
Store.prototype.myNewArrCookie=123;
将由以后创建的对象访问)


长话短说,我认为这里有一个误解,您可以简单地更改代码,以合并封装模式,如和中所述,以获得所需的结果

“调用”?你是说进入吗?为什么不
this.arrCookie
(这里是Store的实例)?不清楚您在问什么。我建议发布“使用函数原型和后续对象创建填充”的示例,以及您希望如何访问阵列。“常规函数”是什么意思?我们称之为属性值,不管它们是否在构造函数中。@Evantimboli不要忘记投票关闭…谢谢您的编辑。是,没有全局
arrCookie
变量。没有任何一个商店实例是它的属性!您必须交换这两个循环—迭代存储,然后为每个存储迭代其cookie。只有这样,您才能知道特定存储区有多少cookie—全局长度变量没有任何意义。或者您只是使用了存储区.arrCookie