Solved

Object scope

Posted on 2006-07-20
9
233 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now