Getting The 'this' Scope Of The Caller

Oct 11, 2006

I'm trying to write an 'each' function for a JavaScript array that
behaves like Ruby's Array#each. (It doesn't matter if you know Ruby to
help with this question.)

My problem is the scope of 'this' inside the iterator callback. I would
like it to be the same as the object that called the each() on the
array. Right now I have to do that with a closure or an
explicitly-passed 'this' scope. For example:

function Person( inName, inCats ) {
this.name = inName;
this.cats = inCats;
}

// Using a closure
Person.prototype.showInfo = function( ) {
var me = this;
this.cats.each( function( catName ){
alert( me.name + " owns " + catName );
} );
}

Array.prototype.each = function( inCallback ){
for ( var i=0,len=this.length; i<len; ++i ){
inCallback( this[ i ], i );
}
}

phrogz = new Person( 'Gavin', [ 'Fuzzles', 'Kitty' ] );
phrogz.showInfo( );
--Gavin owns Fuzzles
--Gavin owns Kitty


// Using an explicit scope
Person.prototype.showInfo = function( ) {
this.cats.each( this, function( catName ){
alert( this.name + " owns " + catName );
} );
}

Array.prototype.each = function( inScope, inCallback ){
for ( var i=0,len=this.length; i<len; ++i ){
inCallback.call( inScope, this[ i ], i );
}
}


Inside the each() function, arguments.callee.caller would give me a
reference to the showInfo function object. What I am looking for is a
way to access the scope of the 'this' receiver within that particular
invocation of showInfo(), so that I can use it in place of inScope
without having to pass 'this' each call.

View 9 Replies


ADVERTISEMENT

Get The Name Of The Caller Object As String?

Oct 19, 2006

is there a way to get the name of the calling object of a method?

function MyFunction()
{
this.SayCallersName = _SayCallersName;
}

function _SayCallersName()
{
alert("how to get 'oTest' as output here?");
}

var oTest = new MyFunction();
oTest.SayCallersName();

What I want as an output is the name of the object I have created
without passing it to the constructor.

So I do not want oTest = new MyFunction("oTest") or something like
that. Is this possible in JScript?

View 1 Replies View Related

Change JS Random Quote To XML Caller?

Oct 21, 2009

I am trying to take code that I have and change it to be called from an XML file.

Right now I have random quote and random author arrays stored in an external .js file. And then a function to make everything random. Then I have it called and modified using the innerhtml method. All of this works fine and I will be including that code.

Now I want to modify this and change it to where I can store my quotes and my quote authors in an xml file. Then called through the JS. (more info under this code)

[Code]...

View 3 Replies View Related

Problems With Objects, Argument.caller And SetTimeout

Jul 23, 2005

the structure of my source code is like this:

<script>
C = function(){
//some initialization
}

C.prototype.start = function{
//some action
setTimeout(arguments.caller.callee, "200");
}

var obj = new C();
obj.start();
</script>

This should execute the method start every 200 milisec, but (in some cases) it gives me an error because arguments.caller is null. Is there a "direct" method to use setTimeout without this arguments? stuff?

BTW: I'm working with IE Version >= 5.0

Is there a better solution to get start work?

View 5 Replies View Related

Possibility Of Saving All Info Related To Caller Number?

Mar 10, 2011

I have to print my cell phone call records but unfortunately my wireless provider displays the number I called only when mouse is hovered over Call details. I have 470 call records and copy pasting all those numbers when mouse is hovered over it will be one hell of a task. Is there any way I could save all the information related to Called Number and do not have to move my mouse for each and every number? Using my basic programming understanding.

(Though I know nothing about javascript), they are using some mouseover function that displays this information. The possibility of this information to be on the server is less because this information is even available when I switch off my wireless. I can paste that portion of the code. Saving this website using normal "Save as" does not save the required information. Is there any other way I can save this website will all its details or somehow disabling this mouseover function so that call details are not hidden anymore.

View 10 Replies View Related

JQuery :: Links Within Load Object Needs To Reference Parent / Caller

Mar 3, 2011

I'm new to jQuery and Javascript overall, I have searched the internet to make use of jQuery instead of the HTML's iframe tag. My problem is links inside a .load file, I want them to refresh the box on the index.php file, not the file itself (in this case the links are in blog.php)

Here's everything I can provide with:
Files:
"index.php"
"blog.php"

In my index.php file I have this script to make my index.php links open the required file in my div box.
"index.php"
<script language="javascript" type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#html").load("blog.php"); // shows the blog.php when I first enter index.php, which I want to have
$(".link_click").click(function(){
$("#html").load($(this).attr("id"));
});
});
</script>

Here's "index.php" link:
<a class="link_click" id="blog.php">Blog</a>

Here's my div box in "index.php" where I show the information within the loaded file:
<div id="html"></div>

All this works, but here's my problem, I have multiple links within "blog.php" which I want to make "index.php" to refresh the "index.php"'s html div. Is there some kind of way to edit the "blog.php"'s script to make it "index.php" the parent or reference?
"blog.php"
<script type="text/javascript">
$(document).ready(function(){
$(".link_click").click(function(){
$("#html").load($(this).attr("id"));
// Need to change this(?) to make it reference the index.php's html div box
});
});
</script>

View 2 Replies View Related

Function.caller If Not Called From Within A Function

Jul 23, 2005

I stumbled over a strange behaviour of Mozilla. When I want to access the
caller property of a function that was not called from within another
function, Mozilla seems to abort the script. No error message, no hang, just
stopping script execution at that point. Why? And what is the remedy?

View 4 Replies View Related

JQuery :: Get Caller Of Function In Function?

Jun 28, 2010

i call a javascript function when click on a href (for hiostorical reasons) and pass this as a parameter.

[Code]...

View 4 Replies View Related

Going Out Of Scope?

Jan 19, 2011

got a problem with this snippet...

function writeColum () {
var x = document.getElementById("wc").value;
var y = document.getElementById("title").value;
if (event.keyCode == 32) {

[Code]...

View 7 Replies View Related

Variable Scope

Jul 20, 2005

If I do . . .

myForm=document.tstForm;
function initialSetup(){
myForm.fld01.value="Test 01"
myForm.fld02.value="Test 02";
myForm.fld01.focus();
}

Then, in the body tag, I do onLoad="initialSetup()",
the script doesn't work and I get a "myForm has no properties" error

I know it'll work if I move it within the function, but I figured a
global variable would retain its value within the function. Why not?

View 2 Replies View Related

Objects And Scope

Jun 26, 2007

In the method nextImage, I can't figure out how to access thumbs. It keeps coming back as undefined. (Using Firefox)

function runPortal(portal_number){
    // there are multiple runPortals on each webpage
    this.portal = document.getElementById('portal'+portal_number); // represents the div that holds the images
    this.thumbs = this.portal.getElementsByTagName('a').length; // represents all the images within the div that will be rotated
    this.length = this.thumbs.length; // that's how many images will be rotated
    // Hide everything
    for (var j=0;j<this.thumbs.length;j++){
        if (j==0) continue; // Don't hide the first one
        this.thumbs[j].childNodes[0].style.display = 'none'
    }
    this.nextImage = function (){
        // there are a fixed number of images to rotate. Start over
        if (this.i >= this.length){
            this.i = 0;
        }
        // One fades away, the next appears
        Effect.dglPuff(this.thumbs[this.last].childNodes[0], {duration:.6, from:.7});
        Effect.Appear(this.thumbs[this.i].childNodes[0]);
       
        // iterate to the next image for the next run
        this.last = this.i;
        this.i++;
    }
    // Set up the image rotator
    // here is where I started guessing
    // thumbs needs to belong to the object rotator, I guess.
   
    this.rotator = new PeriodicalExecuter(this.nextImage, 4); // This object runs the function every 4 seconds
    this.rotator.portal = document.getElementById('portal'+portal_number); // represents the div that holds the images
    this.rotator.thumbs = this.rotator.portal.getElementsByTagName('a'); // represents all the images within the div that will be rotated
    this.rotator.length=this.length;  // that's how many images will be rotated
    this.rotator.i=0; // the counter for what image we're one
    this.rotator.last=0; // the counter for the previous image
   
}

View 5 Replies View Related

Variable Scope And Closures

Jul 23, 2005

Given the following working code:

function attributes() {
var attr1 = arguments[0] || '_'
var attr2 = arguments[1] || '_'
return (
function (el1, el2) {
var value1 = el1[attr1] + el1[attr2];
var value2 = el2[attr1] + el2[attr2];
if (value1 > value2) return 1;
else if (value1 < value2) return -1;
else return 0;
}
);
}

var a = [
{ a:'smith', b:'john' },
{ a:'jones', b:'bob' },
{ a:'smith', b:'jane' }
];
a.sort(attributes('a', 'b'));
for (var i =0; i < a.length; i++) {
document.write(a[i].a + ', ' + a[i].b + '<br>');
}

My question is, are attr1 and attr2 guaranteed to exist through
the lifetime of a.sort(attributes('a', 'b'))?

As I understand it, the anonymous inner function reference I am
returning is a property of attributes(). As such, when I return a
reference to the anonymous inner function, the outer attributes()
function must continue to exist (as must attr1 and att2) until
there are no further references to the inner anonymous function.

As a result, there is no danger of attr1 or attr2 "disappearing"
during the repeated calling of the anonymous inner function.

Is my explanation basically correct, or am I deluding myself and
I'm just lucky that the garbage collector hasn't recovered attr1
or attr2 while the sort is still going on? In other words, is the
behaviour I'm seeing consistent and predictable, or should I
change my approach?

View 3 Replies View Related

Scope Of Event Handlers?

Jul 23, 2005

I have a script in which a function launched by a START button
continuously calculates and writes a value to a text box. The
calculation is done in a for loop. In the loop is a conditional that is
a global variable, a boolean. If the boolean is true, break ends the
loop (or is supposed to!). A STOP button has an onclick function that
sets the global variable to true.

What happens, though, is that the function for the STOP button is
not executed until the for loop reaches the maximum value set for i.
Anyone know how you can get one button to stop a process started by
another?

View 4 Replies View Related

Scope And Object Question

Apr 6, 2006

I had a need for a two dimentional array. Looking for this solution, I
ran accross a statement than all Javascipt arrays were arrays of
objects. So I created a function prototype, at least thats what I was
calling it:

function objRow(vartype, varaddr1, varaddr2)
{
this.type = vartype;
this.addr1 =varaddr1;
this.addr2 =varaddr2;
}

Next I did:
var myobject=new objRow("1", "1234 Main St.", "Apt 101");

At this point I was able to see myobject.addr1 or any other variable in
the object instance.

Now I added this object to a table.
var aryTestTable= new Array();
aryTestTable[0]= myobject;
At this point I could see
aryTestTable[0].addr1
Next I tried an additional object
myobject=new objRow("1", "1234 Main St.", "Apt 101"); //with
different data
And added it to the table
aryTestTable[1]= myobject;
Where I could see:
aryTestTable[1].addr2 or any other variable.

so far so good. Then I started the actual application code where I was
reading a database table and creating the objects and adding them to
the table. This was in a for loop wherein the myobject=new objRow("1",
"1234 Main St.", "Apt 101"); was instantiated.

After the for loop was finished, I could not access the data in the
table - undefined.

So my questions are: Have the my object instances popped off the stack?
and What is the alternative way to implement this table of rows of
values.

View 4 Replies View Related

JQuery :: Scope After AjaxForm?

Apr 4, 2011

I'm showing a form in a Simplemodal dialog in combination with ajaxForm() to redirect the resulting page to another DOM element. The success function of ajaxForm() closes the modal dialog.The resulting page in the other DOM element has no access to the jquery function $(). When I load a page using ajax into the DOM element there is no issue access the jquery function, this only happens when I redirect the resulting page from the ajaxForm() function.

View 3 Replies View Related

Mozilla And Global Scope

Jan 22, 2005

I've just realized that in Mozilla pointer variables always have local scope in a function. Unlike IE. I wondered if Mozilla was able to do it in some other way? readXML() is an init() function which might be a constraint - I'm no javascript expert.

// The following won't work in Mozilla.

var record;

function readXML()
{
record=xmlDoc.getElementsByTagName("record");
}

alert(record[0].childNodes[1].firstChild.nodeValue);

View 4 Replies View Related

Scope Inside A Prototyped Function?

Sep 8, 2006

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? Code:

View 1 Replies View Related

Variable Scope Inside A Function

Oct 30, 2006

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 function, it is global
regardless of whether it's declared with or without "var"
* When it is declared inside a function, if declared with "var", it's
local, if not, it's global
* A local variable that is declared inside a function is local to the
whole function, regardless of where it is declared, e.g.:

function blah() {
for(var i ... ) {
var j ...
}}

i and j will both be visible within blah() after their declaration.
* the notion of "function" in this context also applies for this kind
of construct:

var myHandler =
{
onClickDo: function()
{

in the sense that whatever one declares inside onClickDo with "var"
will only be visible inside onClickDo. What else, am I missing anything?

View 4 Replies View Related

Safari Scope Problem With Dynodes

Jan 5, 2007

I've discovered a scenario where Safari 1.3 (I need to make my stuff
compliant with 1.3+) gets confused about the scope of local variables
WITHIN functions that were created in dynamic script blocks. I've made
this example where function def has a local i variable in a loop, and
it calls function abc which also has a local i variable in a loop. What
happens is that Safari is not respecting the scope and is allowing the
called function to corrupt a local variable in the parent function

Here's the whole test page including html tags. If you try it you'll
see that IE and Gecko both produce the output "in abc" twice, because
the def function correctly gets to call abc twice. On Safari, i gets
corrupted, and abc only gets called once... Any ideas what I can do to
prevent this? Code:

View 8 Replies View Related

JQuery :: Selector Within Local Scope

Mar 8, 2010

How this can be done in jquery,

Let me explain the question using an example:

<html>

If you run it, the alert messageis "pic2", so jquery sees the entire document, but is there a way to easily restrict it to the sub-tree under the current node (in this case the sub-tree under the span node, since that's what was clicked)? Yes, I can do something like alert($("#div1 img:eq(1)").attr("alt")); //undefined as expected

But I am looking for a solution that's more dynamic, so I don't need to hard code #div1.

View 3 Replies View Related

Object Loses Scope With Onchange?

Mar 10, 2009

I will do my best to explain this one and sorry if the title isn't that great. I am trying to write a javascript object and it is my first time, so it isn't that great and of course I have trouble.The object is suppose to populate a dropdown (popMake()) and then add an onchange event (checkValue()) to it. It seems to do this fine but when the select box is changed, I no longer have access to object, it's parameters, etc. In checkValue I don't have access to this.currentMake which was just set or anything (already said that).I believe it has something to do with scope (possibly closure, but I didn't see how it would fit her). So how can I do something like this and still have access to the object after the click?Code below:Code:

window.onload = function() {
var mm = new makeModel();
mm.popMake();

[code]....

View 6 Replies View Related

Onfocus Closure Scope Error

Aug 19, 2006

In my UI framework, I have an event handler - just like many frameworks do. My handler is a static object, and contains methods to take care of things like mouse events, etc. However, upon adding a method to handle onfocus today, I ran into a very odd problem in Firefox. I've put together an example page that generates the error:

View 14 Replies View Related

Using The Switch Statement And Variable Scope?

Jun 14, 2010

I've created a jQuery script that uses a switch statement. However, my experience with it, relative to variable scope, doesn't seem to follow the logic.According to the JavaScript/jQuery theory, a global variable was accessible (meaning read & write) throughtout any function within any script (one that page).However, apparently that theory wasn't completely true as it pertained to switch statements that contained variables. To illustrate my case in point, I've included a simplistic version of my code:

$("#selector").delegate("div", "click", function(event) {
var testVar = 4;
switch (this.id) {

[code]...

As shown, the variable "testVar" is not accessible from one case to the next case .Furthermore, to add insult to injury, I am seeing the same behavior within the conditional if else statement counterpart to the switch statement.

View 1 Replies View Related

Objects & Event Listeners & Scope,

Aug 11, 2004

Here's the situation: I have a javascript object for controlling a custom DHTML scrollbar. So that I can use more than one on a page, the event listeners need to be passed a reference to the particular instance of the object that each needs to connect to, but as I discovered the hard way, inside an event listener, 'this' returns a reference to the DOM object throwing the event, rather than to the JS object. Short of coming up with a linked list of different objects and having the event handler search through it for the right object when an event is generated, then writing a reference to that object to some global variable, is there any convenient way to tie this together? I hope I've made myself clear enough...

View 5 Replies View Related

Scope Issues With My Javascript Object

Mar 26, 2006

I'm having trouble getting the following code to work properly. Every time I try to access the private testing variable from the priveleged MyMethod it gives an error. Says it can't find testing and that it has no properties so I can't run a push() command on it.

function MyClass()
{
var testing = new Array();

// define the method
function MyMethod()
{
this.testing.push("hello");
}

// make the method priveledged
this.MyMethod = MyMethod;
}

// a test function it ensure the variables declared here are isolated
function Start()
{
var myClass = new MyClass();

myClass.MyMethod();

document.write("[" + myClass.testing + "]");
}

Start();

View 3 Replies View Related

Scope With Onclick Iteration Within An Object?

Dec 13, 2011

i'm having a slight problem understand how to use this.myVar in an object. And I use prototype.

[Code]...

Does anyone know how I can use this.myvar within the function. I have tried binding and bindAsEventListener.But nothing I've done has been able to get the right value!

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved