1

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?

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!

4 Answers 4

3

{} 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() {...}
}
Sign up to request clarification or add additional context in comments.

Comments

0

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();

Comments

0

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.

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.