Solved

Object scope

Posted on 2006-07-20
9
240 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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

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!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

623 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