Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Object scope

Posted on 2006-07-20
9
Medium Priority
?
243 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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 800 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 1200 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

876 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