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 05-03-2013, 11:55 AM   PM User | #1
Kyle123
New Coder

 
Join Date: Jan 2013
Posts: 10
Thanks: 3
Thanked 1 Time in 1 Post
Kyle123 is an unknown quantity at this point
Javascript scope in multiple object instances

Hi,

My brain's hurting a bit, would anyone be kind enough to explain why this:
Code:
var myObj = function() {
	var value;
	init = function() {
		value = 10;
		function1();
	}
	function1 = function() {
		alert(value);
	}
	return {
		init : init
	}
};

var inst1 = myObj();
inst1.init();

var inst2 = myObj();
inst2.init();
Works, but this:
Code:
var myObj = function() {
	var value;
	init = function() {
		value = 10;
		function1();
	}
	function1 = function() {
		alert(value);
	}
	return {
		init : init
	}
};

var inst1 = myObj();
var inst2 = myObj();
inst1.init();
inst2.init();
Doesn't?

Thanks in advance

Last edited by Kyle123; 05-03-2013 at 03:01 PM..
Kyle123 is offline   Reply With Quote
Old 05-03-2013, 02:12 PM   PM User | #2
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 358
Thanks: 3
Thanked 43 Times in 43 Posts
Airblader can only hope to improve
I think you're missing the "new" keyword when initializing your instances.
Airblader is online now   Reply With Quote
Old 05-03-2013, 02:36 PM   PM User | #3
Kyle123
New Coder

 
Join Date: Jan 2013
Posts: 10
Thanks: 3
Thanked 1 Time in 1 Post
Kyle123 is an unknown quantity at this point
Thanks for your response, but it doesn't make any difference I'm afraid
Kyle123 is offline   Reply With Quote
Old 05-03-2013, 02:49 PM   PM User | #4
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 358
Thanks: 3
Thanked 43 Times in 43 Posts
Airblader can only hope to improve
You initialize your inner functions at global, which causes interference. Make them local variables, too:

Code:
var myObj = function() {
	var value;

	var init = function() {
		value = 10;
		function1();
	};

	var function1 = function() {
		alert(value);
	};

	return {
		init : init
	}
};

var inst1 = new myObj();
var inst2 = new myObj();
inst1.init();
inst2.init();
Airblader is online now   Reply With Quote
Users who have thanked Airblader for this post:
Kyle123 (05-03-2013)
Old 05-03-2013, 03:00 PM   PM User | #5
Kyle123
New Coder

 
Join Date: Jan 2013
Posts: 10
Thanks: 3
Thanked 1 Time in 1 Post
Kyle123 is an unknown quantity at this point
Oh yeah, that was slack

always the simple things. Thanks
Kyle123 is offline   Reply With Quote
Reply

Bookmarks

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 12:16 PM.


Advertisement
Log in to turn off these ads.