Jump to content

[OOP] Define property from method


flash gordon

Recommended Posts

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.
:)
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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.
:)
Link to comment
Share on other sites

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.
:)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.