flash gordon Posted January 14, 2007 Share Posted January 14, 2007 I'll post the code first[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script language="javascript" type="text/javascript"><!-- function Browser() { this.name = navigator.appName; this.version = navigator.appVersion; this.userAgent = navigator.userAgent; this.brand = brand; } function brand() { var _brand; if (this.userAgent.indexOf("FireFox")) _brand = "FireFox"; else if (this.userAgent.indexOf("MSIE")) _brand = "Internet Explorer"; return _brand; }//--></script></head><body ><script language="javascript"><!-- var currentBrowser = new Browser(); alert("brand " + currentBrowser.brand);//--></script>Hello world.</body></html>[/code]So basically I'm trying to assign a value to property "brand" from a method. My sytnax is incorrect as the output is only the function and not the result.Cheers.:) Quote Link to comment Share on other sites More sharing options...
emehrkay Posted January 15, 2007 Share Posted January 15, 2007 check out the mootools.net library Quote Link to comment Share on other sites More sharing options...
irken Posted January 15, 2007 Share Posted January 15, 2007 Maybe you mean something like this.. (altho this outputs Firefox for Internet explorer??). Might not be how to aproach it, I'm not sure, but this is how I do most of my things.[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script language="javascript" type="text/javascript">function Browser() { this.name = navigator.appName; this.version = navigator.appVersion; this.userAgent = navigator.userAgent;};Browser.prototype = { brand: function() { var _brand; if (this.userAgent.indexOf("FireFox")) _brand = "FireFox"; else if (this.userAgent.indexOf("MSIE")) _brand = "Internet Explorer"; return _brand; } }; Browser</script></head><body ><script language="javascript"><!-- var b = new Browser(); alert(b.brand());//--></script>Hello world.</body></html>[/code] Quote Link to comment Share on other sites More sharing options...
flash gordon Posted January 15, 2007 Author Share Posted January 15, 2007 yea, I'm not too worried about the result, just the structure of the class. Basically, I'd like to call a property of "Browser" and not a method. So while [i]b.brand()[/i] would work, I'm really looking just to call [i]b.brand[/i]. Does that make sense?So I'd like to return the result of a "private" method to a property and just access the property rather than the method. I'm not even sure if this is possible with prototypes or not....Cheers.:) Quote Link to comment Share on other sites More sharing options...
flash gordon Posted January 16, 2007 Author Share Posted January 16, 2007 So as the code turns out:[code]<!-- normal headers --><script language="javascript" type="text/javascript"><!-- /* * Class to sniff clients browser for stats * * Constructor to define properties */ function Browser() { this.name = navigator.appName; this.brand = brand(); // scope of class lost this.testME = test; // scope retained, but testME is only a reference to method test. } function test() { alert(this.name); } /* * private method used to retrieve brand name of browser * * @return browser brand name */ function brand() { var _brand; if (this.navigator.userAgent.indexOf("FireFox") != -1) _brand = "FireFox"; else if (this.navigator.userAgent.indexOf("MSIE") != -1) _brand = "Internet Explorer"; return _brand; } function blurred() { this.focus(); } //--></script></head><body ><script language="javascript"><!-- var currentBrowser = new Browser(); currentBrowser.testME(); for (var i in currentBrowser) { document.write( i + " = " + currentBrowser[i] + "<br />"); //--></script>...[/code]So I suppose my real issue was scope of the class getting lost if I set the this.brand = brand(); is where I JS errors were.Thanks everyone.Cheers.:) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.