简体   繁体   中英

How can I access a data from another object in JavaScript?

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!

{} 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() {...}
}

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

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.

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

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