[英]How can I access a data from another object in JavaScript?
I want to access availJobs
in jobs.scan
object but I couldn't.我想在
jobs.scan
object 中访问availJobs
,但我不能。 It is not defined in jobs.attack
.它没有在
jobs.attack
中定义。 What can I do to access the part of jobs.scan
in other objects?我该怎么做才能访问其他对象中的
jobs.scan
部分?
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. jobs.attack 中的
jobs.attack
availJobs[0]
不起作用。 It's undefined
.这是
undefined
的。 How can I set the availJobs
as public and can be accessed in other objects?如何将
availJobs
设置为公共的并且可以在其他对象中访问?
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. {}
用于初始化一个object,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 ,
:在
{}
声明中,您可以将多个成员添加到您的 object,如果您用逗号,
分隔它们:
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();
我在这里可能是错的,但我很确定你需要在 function 本身之外声明 availJobs,即:
jobs.availJobs = new Array();
Your code is incorrect.您的代码不正确。
jobs.scan
is one function, jobs.attack
is another. jobs.scan
是一个 function, jobs.attack
是另一个。 availJobs
is a local variable defined in jobs.scan
. availJobs
是在jobs.scan
中定义的局部变量。 You can't access local variables of one function from another.您不能从另一个访问一个 function 的局部变量。
Even more, availJobs
doesn't exist by the time you try to access it, because jobs.scan is already finished.更重要的是,当您尝试访问它时,
availJobs
并不存在,因为 jobs.scan 已经完成。
You need to declare the availJobs array and jobs should be an object.您需要声明 availJobs 数组,作业应为 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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.