tags:

views:

57

answers:

4

Hi,

I want to access availJobs in jobs.scan object but I couldn't. It is not defined in jobs.attack. What can I do to access the part of jobs.scan in other objects?

var jobs = new Array();

jobs.scan = function() {
    var availJobs = new Array();
    var jobContents = dom.get("app8743457343_content");
    var boldthreads = jobContents.getElementsByTagName('b');
    for(var i = 0; i < boldthreads.length; i++) {
        if(boldthreads[i].style.color == 'silver') {
            availJobs.push(boldthreads[i].textContent);
        }
    }
    return availJobs;
}

jobs.attack = function() {
    jobs.scan();
    alert(jobs.availJobs[0]);
}

jobs.attack();

availJobs[0] in jobs.attack doesn't work. It's undefined. How can I set the availJobs as public and can be accessed in other objects?jav

Thanks for all the help!! Here's the code that I put:

var jobs = {
    availJobs: new Array(),
    scan: function() {
        var jobContents = dom.get("app8743457343_content");
        var boldthreads = jobContents.getElementsByTagName('b');
        for(var i = 0; i < boldthreads.length; i++) {
            if(boldthreads[i].style.color == 'silver') {
                this.availJobs.push(boldthreads[i].textContent);
            }
        }
    },
    attack: function() {
        this.scan();
        alert(this.availJobs[0]);
    },
};

jobs.attack();

This code is definitely more elegant don't you think? I used this and it worked!

A: 

I may be wrong here, but I'm pretty sure you need to declare availJobs outside of the function itself, IE: jobs.availJobs = new Array();

Alex Fort
+4  A: 

{} is used to initialize an object and Array to initialize an array.

var jobs = {
    availJobs : new Array()
}

jobs.scan = function() {
    var jobContents = dom.get("app8743457343_content");
    var boldthreads = jobContents.getElementsByTagName('b');
    for(var i = 0; i < boldthreads.length; i++) {
        if(boldthreads[i].style.color == 'silver') {
            availJobs.push(boldthreads[i].textContent);
        }
    }
    return availJobs;
}

In a {} declaration, you can put add multiple members to your object if you separe them with a comma (,) :

var jobs = {
    availJobs : new Array(),
    anotherMember : null,
    anotherArray : new Array(),
    aFunction = function() {...}
}
ybo
A: 

Your code is incorrect. jobs.scan is one function, jobs.attack is another. availJobs is a local variable defined in jobs.scan. You can't access local variables of one function from another.

Even more, availJobs doesn't exist by the time you try to access it, because jobs.scan is already finished.

Eugene Morozov
A: 

You need to declare the availJobs array and jobs should be an object.

var jobs = {}

jobs.availJobs = []

jobs.scan = function() {
    var availJobs = new Array();
    var jobContents = dom.get("app8743457343_content");
    var boldthreads = jobContents.getElementsByTagName('b');
    for(var i = 0; i < boldthreads.length; i++) {
        if(boldthreads[i].style.color == 'silver') {
            availJobs.push(boldthreads[i].textContent);
        }
    }
    return availJobs;
}

jobs.attack = function() {
    jobs.scan();
    alert(jobs.availJobs[0]);
}

jobs.attack();
chills42