Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-26-2010, 07:19 PM   PM User | #1
jschrend
New to the CF scene

 
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
jschrend is an unknown quantity at this point
Smile Nested Methods and Properties in JavaScript

Hi all! I'm pretty new to JavaScript and completely new to this forum. I'm James.

Well uh, I'm not sure how to...

I wanted to make something like this using javascript...

Code:
myObject.someMethod('something').anotherProperty
much like the

Code:
document.getElementById('someId').style.morestuff
But the thing is, I've been around every DOM whatnot I can find, I just can't seem to make it work.

I kinda need it to make my own DOM for some API i'm writing. So I can write like...

Code:
function someGreatAPI(){
     this.property1='';
     this.property2='';
     //...
     this.method1 = function(var1){            //this should make my firstMethod
           this.nestedProperty = 0;
           this.nestedMethod = someFunction;  //firstMethods nested method

     }
}

//so I can use it like...

var obj = new someGreatAPI();
obj.method1(10).nestedMethod();
It has to be possible, it's possible with document.getelementbyid, right? it has to be.

Thanks in advance.

Last edited by jschrend; 04-26-2010 at 10:07 PM.. Reason: Solved!
jschrend is offline   Reply With Quote
Old 04-26-2010, 08:28 PM   PM User | #2
gusblake
Regular Coder

 
Join Date: Jan 2006
Posts: 568
Thanks: 6
Thanked 84 Times in 84 Posts
gusblake is on a distinguished road
The reason it works with document.getElementById is because getElementById returns an HTML element.

obj.method1 doesn't return anything, so obj.method1(10).nestedMethod() is resolved as obj.(undefined).nestedMethod()

Since method1 is a constructor, you would do it like this:

Code:
var testObj=new obj.method1(10);
testObj.nestedMethod();
Edit - to make it work in exactly the same way you would have to do something like this:

Code:
function someGreatAPI(){
	this.property1='';
	this.property2='';
	//...
	
	this.method1 = function(var1) {
		this.nestedProperty = 0;
		this.nestedMethod = function() {
			alert(var1);
		};
	}
	
	this.createNewM1=function(n) {
		return new this.method1(n);
	}
}

var obj = new someGreatAPI();

obj.createNewM1(10).nestedMethod();

Last edited by gusblake; 04-26-2010 at 08:43 PM..
gusblake is offline   Reply With Quote
Old 04-26-2010, 10:06 PM   PM User | #3
jschrend
New to the CF scene

 
Join Date: Apr 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
jschrend is an unknown quantity at this point


It has to return an object, I see. Thanks man, that's exactly what I needed.





jschrend is offline   Reply With Quote
Reply

Bookmarks

Tags
document object model, javascript, method, nested

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:43 PM.


Advertisement
Log in to turn off these ads.