开发者

Can't get JavaScript Array to work in OOP style

开发者 https://www.devze.com 2023-01-16 07:38 出处:网络
Hi currently I\'m trying to get following snippet of code 开发者_开发问答to work: function Entry() {

Hi currently I'm trying to get following snippet of code 开发者_开发问答to work:

function Entry() {
    var pauses = new Array();
}

Entry.prototype = {
   AddElement: function(aParameter) {
      this.pauses.push(aParameter);
   }
}

Unfortunately this code fails with following error in Safari if I try to call AddElement("Test");

TypeError: Result of expression 'this.pauses' [undefined] is not an object. Does anybody know why?


In your code, pauses is a local variable within the Entry() function, not a member on the object constructed by it.

You want to replace var pauses = ... with this.pauses = ....


change

var pauses = new Array();

to

this.pauses = new Array();

or, better

this.pauses = [];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号