/    Sign up×
Community /Pin to ProfileBookmark

JSON help – accessing parent’s object functions

[CODE]var cjs = function(){
function RtnObjWant(el){
return (typeof(el) == ‘object’) ? this.Obj = ele : this.Obj = document.getElementById(el);}
return {
RtnObj : function(el) {
return (typeof(el) == ‘object’) ? this.Obj = ele : this.Obj = document.getElementById(el);},
getById : function(el) {
return document.getElementById(el);}
};
}();
cjs.sh = function() {
return {
ShowIt : function(el) {
this.RtnObjWant(el).style.display=”;}, // Doesnt Work
HideIt : function(el) {
cjs.RtnObj(el).style.display=’none’;} // Works but is there a way to access it with using this instead?
};
}();

cjs.css = function() {
etc….[/CODE]

Question is above in the code but ill rephrase here. How can i call another sub-objects function without using the variable name that it was originally created under, like using this.parent or something.

Like if i wanted to call a function under another sub-object like from the sh sub-object to the css sub-object. I tryed with this.parent.css and that didnt work.

The RtnObjWant function is a private function i believe under the main object so i don’t think other sub-objects can touch that function but I know they can hit the other functions that are not private using the variable name, in this case ‘cjs’, but i want a more dynamic way in case i have to change the original variable name to something besides cjs.

Thanks in advance.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@toicontienApr 08.2009 — You'll need to store a reference to the parent object. Basically, a property of the sub object that points to the parent object:
[CODE]var foo = {};

foo.subObj = {};

foo.subObj.parentObj = foo;[/CODE]


Be careful as this creates a circular reference between two objects. Creating circular references to DOM nodes causes IE6 to leak memory like the Titanic simultaneously hit by an iceberg and a barrage of torpedoes.
×

Success!

Help @Tinypants 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.16,
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,
)...