Solved

Object scope

Posted on 2006-07-20
9
234 Views
Last Modified: 2012-06-27
MyObject = {
               objectProperty:"HELLO",

      applyAction : function(item) {
              item.onmouseover = this.doOver;
      },
      
      doOver : function() {
             alert(objectPropert);      
      }      
}


with this code, javascript alerts "undefined". How can i make javscript to alert objectProperty?
0
Comment
Question by:blacklord
9 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17146943
Hi blacklord,

var objProperty = "";

MyObject = {
               objectProperty:"HELLO",

     applyAction : function(item) {
             item.onmouseover = this.doOver;
          objProperty = objectProperty;
     },
     
     doOver : function() {
            alert(objPropert);    
     }    
}


Apresto
0
 

Author Comment

by:blacklord
ID: 17146997
yes that works, thank you, but i am trying to write an easy to maintain and easy to use everywhere object. Using a global variable has some disadvantages and dangers. Isn't there a way other than using global variables?
0
 
LVL 23

Expert Comment

by:apresto
ID: 17147043
not unless you pass the object into a function that alerts:

MyObject = {
               objectProperty:"HELLO",

     applyAction : function(item) {
             item.onmouseover = this.doOver;
             showMsg(objectProperty);
               },
}

     function showMsg( obj )
   {  
         alert( obj );
   }
0
 
LVL 23

Expert Comment

by:apresto
ID: 17147053
but they arent that dangerous, granted you shouldnt over use them.  I normally give an object global scope in the page if i know for a fact i will be reusing it from multiple functions
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:blacklord
ID: 17147169
but i am assigning different properties to different items. that way, i will have to create a different global variable for each item
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 200 total points
ID: 17147180
Here my proposal:

<script>

function MyObject() {
  this.objectProperty="HELLO";
  this.applyAction = function(item) {
      item.objRef = this;
      item.onmouseover = this.doOver;
  };
     
  this.doOver = function() {
      alert(this.objRef.objectProperty);    
  };    
}

</script>
<body>
<img name="myImg" src="http://www.experts-exchange.com/images/certification.gif">
<a href="#" onClick="var xObj=new MyObject();xObj.applyAction(document.images.myImg);return false;">Set Action</a>
</body>

0
 

Author Comment

by:blacklord
ID: 17148486
hi zvonko, thanks bu doesn't work too.

function List (table)      {
      this.itemsTD = table.getElementsByTagName("td");
      this.applyActions();
}

OneColumnList.prototype.applyActions = function () {
      for (var i=0;i<this.itemsTD.length;i++) {
            this.itemsTD[i].setAttribute("objRef", this);
            this.itemsTD[i].onmouseover = List .doOver;
      }
}

List .doOver = function () {
      var obj = this.getAttribute("objRef");
      alert(obj.listName);
}
0
 

Author Comment

by:blacklord
ID: 17148513
sorry, here is the code: (still doesn't work)

function List (table)     {
     this.itemsTD = table.getElementsByTagName("td");
     this.applyActions();
}

List .prototype.applyActions = function () {
     for (var i=0;i<this.itemsTD.length;i++) {
          this.itemsTD[i].setAttribute("objRef", this);
          this.itemsTD[i].onmouseover = List .doOver;
     }
}

List .doOver = function () {
     var obj = this.getAttribute("objRef");
     alert(obj.listName);
}
0
 
LVL 2

Accepted Solution

by:
zeroreality earned 300 total points
ID: 17149091
don't use setAttribute for anything except string values.


function List (table)     {
     this.itemsTD = table.getElementsByTagName("td");
     this.applyActions();
}

List.prototype.applyActions = function () {
     for (var i=0;i<this.itemsTD.length;i++) {
          this.itemsTD[i].objRef = this;
          this.itemsTD[i].onmouseover = List.doOver;
     }
}

List .prototype.doOver = function () {
     alert(this.objRef.listName);
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now