Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Object scope

Posted on 2006-07-20
9
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

715 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