我试图访问类的一个成员变量,它是类的成员函数中的数组,但是得到了一个错误:
无法读取未定义的属性
'length'
类:
function BasicArgs(){
var argDataType = new Uint8Array(1);
var argData = new Uint32Array(1);
}成员职能:
BasicArgs.prototype.getByteStreamLength = function(){
alert(this.argData.length);
return i;
}这是其中一个例子,但我在很多地方都遇到过这种情况。像整数这样的变量很容易访问,但大多数情况下问题是数组。我会感谢你的帮助。
发布于 2012-10-18 14:33:01
您需要this在构造函数中创建对象的属性。
function BasicArgs(){
this.argDataType = new Uint8Array(1);
this.argData = new Uint32Array(1);
}原型函数无法直接访问构造函数的变量范围。
然后确保使用new调用构造函数。
var ba = new BasicArgs();
ba.getByteStreamLength();发布于 2012-10-18 14:33:31
可以访问函数的私有变量。
修改后的代码:
function BasicArgs(){
this.argDataType = new Uint8Array(1);
this.argData = new Uint32Array(1);
}
BasicArgs.prototype.getByteStreamLength = function(){
alert(this.argData.length);
return i;
}发布于 2012-10-18 14:34:11
声明var argData不会在对象上创建属性。它只创建一个局部变量,构造函数一退出就会消失。你需要做的
this.argData = new Uint32Array(1)
而不是。
https://stackoverflow.com/questions/12957045
复制相似问题