简体   繁体   中英

Javascript array is undefined… and I'm not sure why

I'm trying to translate a PHP class into 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 . 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. You are right on the money creating a that variable, use it on *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/

This is actually pretty complex and I believe someone with more experience in Javascript may be able to clarify the situation.

Edit2: Douglas Crockfords explains it with these words:

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.

To see the complete article head to: http://javascript.crockford.com/private.html

You never call 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? 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:

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/ .

It's working fine for me: http://jsfiddle.net/YznSE/6/ You just didn't call 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM