/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Variables inside methods and ojbects

I have this code, and it returns an error (firebug) on line 26, saying that the variables all_tags is not defined.

I am a little confused about this. Any explanation and help would be appreciated. Thanks in advance. Here is the code:

[CODE]
window.onload = initAll;

function initAll() {
Z.getAllTags();
Z.showNumOfTags();
}

var Z = {

getAllTags: function() {

var all_tags = [];
if (typeof document.all != ‘undefined’) {
all_tags = document.all;
}
else {
all_tags = document.getElementsByTagName(‘*’);
console.debug(all_tags.length);
}

var tag_num = all_tags.length;
console.debug(‘From getAllTags ‘ + tag_num);
},

showNumOfTags: function() {
var num_tags = all_tags.length;
console.debug(‘From showNumOfTags ‘ + num_tags);
}
};

/* And it says ‘all_tags is not defined’ – line 26. */

[/CODE]

And just in case, I also posted it [URL=”http://pastebin.com/GfJdztQK”]here.[/URL]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Dec 09.2010 — Because when you run showNumofTags it isn't defined. Change to this.all_tags = [];

But there really is absolutely no need to use document.all anymore, unless you very specifically need to support IE 5.0 or less. But literally nobody uses that anymore.
Copy linkTweet thisAlerts:
@FernandoBassoauthorDec 09.2010 — Because when you run showNumofTags it isn't defined. Change to this.all_tags = [];

Thanks. I sure worked fine, although I have not really understood why.

I know the keyword [B]this[/B] refers to the element that triggers a function/method to run. I do not know about the use of [b]this[/b] in this very case.

But there really is absolutely no need to use document.all anymore, unless you very specifically need to support IE 5.0 or less. But literally nobody uses that anymore.[/QUOTE]


Thanks. I was not aware of that. I am studying the book Simply JavaScript - 2007 - and it mentions about it. It is a relief to know that it is not needed anymore.
Copy linkTweet thisAlerts:
@FernandoBassoauthorDec 09.2010 — Sorry. I missquoted the text....
×

Success!

Help @FernandoBasso spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...