473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Scope inside a prototyped function?

I'm trying to access some of the global's inside my class LiveSearch
and I have no idea how to go about it. Here is what I have so far:

<script type="text/javascript" src="query.js"></script>
<script type="text/javascript">
function LiveSearch(global) {
this.theglobal = global;
this.initialize();
}

LiveSearch.prototype.initialize = function() {
$("#thebutton").mousedown(function() { //when we click the button
alert(this.theglobal);
});
}

$(document).ready(function() {
var objSearch = new LiveSearch("globalvalue");
});
</script>

On page load I create a new LiveSearch instance and it assigns
theGlobal = "globalvalue" and proceeds to initialize(); At this point
Im using JQuery to setup an onmousedown event on a button on my page
with id="thebutton". When I click the button the alert comes back with
'undefined'. How can I get direct access to my theglobal variable?

The only way I have found so far is by adding a local variable to my
initialize() func and alerting the local variable:

LiveSearch.prototype.initialize = function() {
var theglobal = this.theglobal;
$("#thebutton").mousedown(function() { //when we click the button
alert(theglobal);
});
}

Now this works fine, but Im only accessing a local copy of theglobal,
so I cannot alter its value. (setting theglobal = "somethingelse"; only
alters my local copy, not the one specified in my class constructor).
Is there a way to do this?

The answer is probably all to obvious, but Im beating my head against a
wall here trying to come up with the answer - thanks ahead of time guys!

Sep 8 '06 #1
1 1511
BrianMH wrote:
I'm trying to access some of the global's inside my class LiveSearch
and I have no idea how to go about it. Here is what I have so far:

<script type="text/javascript" src="query.js"></script>
<script type="text/javascript">
function LiveSearch(global) {
this.theglobal = global;
this.initialize();
}

LiveSearch.prototype.initialize = function() {
$("#thebutton").mousedown(function() { //when we click the button
alert(this.theglobal);
});
}

$(document).ready(function() {
var objSearch = new LiveSearch("globalvalue");
});
</script>

On page load I create a new LiveSearch instance and it assigns
theGlobal = "globalvalue" and proceeds to initialize(); At this point
Im using JQuery to setup an onmousedown event on a button on my page
with id="thebutton". When I click the button the alert comes back with
'undefined'. How can I get direct access to my theglobal variable?
A function's this operator always refers to the object that it has been
called as a method of. It is not static, its value is determined when
the function is called, each and every time.

When you call LiveSearch() as a constructor, its this operator refers
to the newly created object. When you attach that object as a method of
some DOM object (e.g. an onmousedown handler), and it is called by the
mousedown event, its this operator refers to the DOM object.

The only way I have found so far is by adding a local variable to my
initialize() func and alerting the local variable:

LiveSearch.prototype.initialize = function() {
var theglobal = this.theglobal;
$("#thebutton").mousedown(function() { //when we click the button
alert(theglobal);
});
}
In the above, the outer anonymous function object is given a
'theglobal' property that is the value of the newly constructed
object's theglobal variable (which is whatever 'global' was at the time
it was constructed). When it is executed, that is the value that is
shown.

Now this works fine, but Im only accessing a local copy of theglobal,
so I cannot alter its value. (setting theglobal = "somethingelse"; only
alters my local copy, not the one specified in my class constructor).
Is there a way to do this?
Create 'global' as a public property of the constructor and reference
that. In the following small example, Foo.global is first set to 'bar'
when Foo is called for buttonA, but then changed to 'sod' when it is
called for buttonB.

I've modified your stuff a bit so that Foo works more generically, but
I think in principle it's the same.

<script type="text/javascript">

function Foo(global, id){
this.theGlobal = global;
this.initialise(id);
}

Foo.prototype.initialise = function(id){
Foo.global = this.theGlobal;
document.getElementById(id).onclick =
function(){alert(Foo.global);}
}

window.onload = function(){
new Foo('bar', 'buttonA');
new Foo('sod', 'buttonB');
}

</script>

<input id="buttonA" type="button" value="Button A">
<input id="buttonB" type="button" value="Button B">
<input type="button" value="Button C" onclick="alert(Foo.global)">
Note that Foo.global is accessible from anywhere, as shown by buttonC.
Is that what you wanted?
--
Rob

Sep 9 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
7
by: Christian Christmann | last post by:
Hi, I've a a question on the specifier extern. Code example: void func( void ) { extern int e; //...
4
by: Ray | last post by:
Hello, I think I've had JavaScript variable scope figured out, can you please see if I've got it correctly? * Variables can be local or global * When a variable is declared outside any...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Laurent Deniau | last post by:
I would like to know why the following small program does not compile (checked with gcc 4.1.2) and if the compiler behavior is correct: struct A; typedef void (T)(struct A*); void f(void) {...
21
by: Thomas Pornin | last post by:
Hello, with gcc-4.2.3, this does not compile: typedef int foo; int f(foo) int foo; { return foo; }
27
by: Erwin Moller | last post by:
Hi group, Consider this simple script (tested on FF3): <script type="text/javascript"> test = 'outer'; for (var i=0;i<2;i++){ alert(test); var test = 'inner'; alert (test);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.