[英]Javascript array is undefined… and I'm not sure why
I'm trying to translate a PHP class into JavaScript. 我正在尝试将PHP类翻译成JavaScript。 The only thing I'm having trouble with is getting an item out of an array variable.
我唯一遇到的问题是从数组变量中获取一个项目。 I've created a simple jsfiddle here .
我在这里创建了一个简单的jsfiddle。 I cannot figure out why it won't work.
我无法弄清楚为什么它不起作用。
(EDIT: I updated this code to better reflect what I'm doing. Sorry for the previous mistake.) (编辑:我更新了这段代码,以便更好地反映我正在做的事情。抱歉以前的错误。)
function tattooEightBall() {
this.subjects = ['a bear', 'a tiger', 'a sailor'];
this.prediction = make_prediction();
var that = this;
function array_random_pick(somearray) {
//return array[array_rand(array)];
var length = somearray.length;
var random = somearray[Math.floor(Math.random()*somearray.length)];
return random;
}
function make_prediction() {
var prediction = array_random_pick(this.subjects);
return prediction;
}
}
var test = tattooEightBall();
document.write(test.prediction);
Works fine here, you are simple not calling 在这里工作正常,你很简单不打电话
classname();
After you define the function. 定义函数后。
Update 更新
When you make a call to *make_prediction* , this will not be in scope. 当您调用* make_prediction *时, 这不在范围内。 You are right on the money creating a that variable, use it on *make_prediction* :
你创造一个变量的钱是正确的,在* make_prediction *上使用它:
var that = this;
this.prediction = make_prediction();
function make_prediction() {
var prediction = ''; //initialize it
prediction = prediction + array_random_pick(that.subjects);
return prediction;
}
You can see a working version here: http://jsfiddle.net/zKcpC/ 你可以在这里看到一个工作版本: http : //jsfiddle.net/zKcpC/
This is actually pretty complex and I believe someone with more experience in Javascript may be able to clarify the situation. 这实际上非常复杂,我相信有更多Javascript经验的人可能能够澄清这种情况。
Edit2: Douglas Crockfords explains it with these words: 编辑2:Douglas Crockfords用这些话解释道:
By convention, we make a private that variable.
按照惯例,我们创建一个私有变量。 This is used to make the object available to the private methods.
这用于使对象可用于私有方法。 This is a workaround for an error in the ECMAScript Language Specification which causes this to be set incorrectly for inner functions.
这是ECMAScript语言规范中的错误的解决方法,导致对内部函数的设置不正确。
To see the complete article head to: http://javascript.crockford.com/private.html 要查看完整的文章,请访问: http : //javascript.crockford.com/private.html
You never call classname
. 你永远不会叫
classname
。 Seems to be working fine. 似乎工作正常。
Works for me: 适合我:
(function classname() {
this.list = [];
this.list[0] = "tiger";
this.list[1] = "lion";
this.list[2] = "bear";
function pickone(somearray) {
var length = somearray.length;
var random = somearray[Math.floor(Math.random()*length)];
return random;
}
var random_item = pickone(this.list);
document.write(random_item);
}());
Were you actually calling the classname
function? 你实际上是在调用
classname
函数吗? Note I wrapped your code block in: 注意我将您的代码块包装在:
([your_code]());
I'm not sure what you're trying to accomplish exactly with the class structure you were using so I made some guesses, but this code works by creating a classname object that has instance data and a pickone
method: 我不确定你要用你正在使用的类结构完成什么,所以我做了一些猜测,但这段代码的工作原理是创建一个包含实例数据和
pickone
方法的classname对象:
function classname() {
this.list = [];
this.list[0] = "tiger";
this.list[1] = "lion";
this.list[2] = "bear";
this.pickone = function() {
var length = this.list.length;
var random = this.list[Math.floor(Math.random()*length)];
return random;
}
}
var cls = new classname();
var random = cls.pickone();
You can play with it interactively here: http://jsfiddle.net/jfriend00/ReL2h/ . 你可以在这里以交互方式玩它: http : //jsfiddle.net/jfriend00/ReL2h/ 。
It's working fine for me: http://jsfiddle.net/YznSE/6/ You just didn't call classname()
. 它对我来说很好: http : //jsfiddle.net/YznSE/6/你刚才没有调用
classname()
。 If you don't call it, nothing will happen ;) 如果你不打电话,什么都不会发生;)
Make it into a self-executing function like this: 将它变成一个像这样的自执行函数:
(function classname() {
this.list = [];
this.list[0] = "tiger";
this.list[1] = "lion";
this.list[2] = "bear";
function pickone(somearray) {
var length = somearray.length; //<---WHY ISN'T THIS DEFINED??
var random = somearray[Math.floor(Math.random() * length)];
return random;
}
var random_item = pickone(this.list);
document.write(random_item);
})();
var test = tattooEightBall();
document.write(test.prediction);
Should be: 应该:
var test = new tattooEightBall(); //forgot new keyword to create object
document.write(test.prediction()); // forgot parens to fire method
and: 和:
this.prediction = make_prediction();
Should be: 应该:
this.prediction = make_prediction;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.