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 09-17-2010, 10:10 PM   PM User | #1
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
firebug: component.singularize is not a function

Hey all,

Firebug gives me the below error message:

component.singularize is not a function
[Break on this error] var component = component.singularize();

This is what I have:
Code:
		var component = window.location.hash.replace('#', '');
		var component = component.singularize();
		
		alert("The component name is " + component);
		
		
		
		var singularize = function(){
			if(!String.prototype.singularize){
				String.prototype.singularize = function(){
					var context = this;
					result = ((context.charAt(context.length -1, 1) == 's') ? context.substring(0, context.length - 1) : context);
					return result;
				}
			}
		}
Thanks for any response.
johnmerlino is offline   Reply With Quote
Old 09-18-2010, 01:22 AM   PM User | #2
Shaka Zorba
New Coder

 
Join Date: Sep 2010
Location: U S of A
Posts: 55
Thanks: 0
Thanked 11 Times in 11 Posts
Shaka Zorba is an unknown quantity at this point
Code:
<script>var component = window.location.hash.replace('#', '');
		
		
		alert("The component name is " + component);
		
		
		
		var singularize = function(){
			if(!String.prototype.singularize){
				String.prototype.singularize = function(){
					var context = this;
					result = ((context.charAt(context.length -1, 1) == 's') ? context.substring(0, context.length - 1) : context);
					return result;
				}
			}
		}
singularize();
var component = component.singularize();
alert("The component name is " + component);
</script>
Shaka Zorba is offline   Reply With Quote
Old 09-18-2010, 03:27 AM   PM User | #3
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
Quote:
Originally Posted by Shaka Zorba View Post
Code:
singularize();
var component = component.singularize();
Why would you need to call the function (singularize() and then call the function again (component.singularize()?
johnmerlino is offline   Reply With Quote
Old 09-18-2010, 11:00 AM   PM User | #4
Shaka Zorba
New Coder

 
Join Date: Sep 2010
Location: U S of A
Posts: 55
Thanks: 0
Thanked 11 Times in 11 Posts
Shaka Zorba is an unknown quantity at this point
Quote:
Originally Posted by johnmerlino View Post
Why would you need to call the function (singularize() and then call the function again (component.singularize()?
component.singularize() is not a function until
the function singularize() has ran
they are not the same thing at all
Shaka Zorba is offline   Reply With Quote
Old 09-18-2010, 04:24 PM   PM User | #5
johnmerlino
Regular Coder

 
Join Date: Oct 2009
Posts: 189
Thanks: 38
Thanked 3 Times in 3 Posts
johnmerlino is an unknown quantity at this point
Is there a less repetitive way to do this? I guess a class method? Although, I thought that was the whole idea of prototyping it to String object, so I can call it on a string.

Last edited by johnmerlino; 09-18-2010 at 04:28 PM..
johnmerlino is offline   Reply With Quote
Old 09-18-2010, 04:42 PM   PM User | #6
Shaka Zorba
New Coder

 
Join Date: Sep 2010
Location: U S of A
Posts: 55
Thanks: 0
Thanked 11 Times in 11 Posts
Shaka Zorba is an unknown quantity at this point
Quote:
Originally Posted by johnmerlino View Post
Is there a less repetitive way to do this? I guess a class method? Although, I thought that was the whole idea of prototyping it to String object, so I can call it on a string.
<script>var component = window.location.hash.replace('#', '');

alert("The component name is " + component);

if(!String.prototype.singularize){
String.prototype.singularize = function(){
var context = this;
result = ((context.charAt(context.length -1, 1) == 's') ? context.substring(0, context.length - 1) : context);
return result;
}
}
var component = component.singularize();
alert("The component name is " + component);
</script>
Shaka Zorba is offline   Reply With Quote
Old 09-18-2010, 04:52 PM   PM User | #7
Shaka Zorba
New Coder

 
Join Date: Sep 2010
Location: U S of A
Posts: 55
Thanks: 0
Thanked 11 Times in 11 Posts
Shaka Zorba is an unknown quantity at this point
Code:
<script>var component = window.location.hash.replace('#', '');		
		
		alert("The component name is " + component);		
			
				String.prototype.singularize = function(){
					var context = this;
					result = ((context.charAt(context.length -1, 1) == 's') ? context.substring(0, context.length - 1) : context);
					return result;
				}			
var component = component.singularize();
alert("The component name is " + component);
</script>
Shaka Zorba is offline   Reply With Quote
Reply

Bookmarks

Tags
function, javascript, prototype, string

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 10:50 AM.


Advertisement
Log in to turn off these ads.