/    Sign up×
Community /Pin to ProfileBookmark

Javascript as OOP, basics

Hi to all,

I have one strange problem.
This is code I am using.

[CODE]// JavaScript Document
var IFrameDropDown = {
topLeftX : “0px”,
topLeftY : “0px”,
width : “100px”,
height : “200px”,
url : “mvm.php”,
inputField : Object,
init : function()
{
this.inputField = document.getElementById(“whiteplayer”);
this.addEvents(this.inputField, “change”);
},
addEvents : function(obj, e)
{
if(window.addEventListener){ // Mozilla, Netscape, Firefox
obj.addEventListener(e, this.alerter, false);
} else { // IE
obj.attachEvent(“on” + e, this.alerter);
}
},
alerter :function()
{
alert(“sdf”);
},
setTopLeftX:function(x)
{
this.topLeftX = x + “px”;
},
setTopLeftY:function(y)
{
this.topLeftY = y + “px”;
}
};
window.onload = IFrameDropDown.init;[/CODE]

I get this error message:

this.addEvents is not a function

in line

this.addEvents(this.inputField, “change”);

When I put IFrameDropDown instead of this it is working fine.
What I am doing wrong?

Thanks in advance.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@mavigozlerAug 29.2009 — The 'this' keyword within the scope of a constructor (function) would refer to the object being constructed, as you might expect.

But your scope is not within a constructor, but rather an associative object. The reference to 'this' should be to the global object, generally the 'window' object. Thus any assignment you make with 'this' get assigned to the global/window object. But the moment you try to [I]refer[/I] to a property or method (invoke a function), you get the error that the property or method was not previously defined. You defined that function 'addEvents' as part of the associative array IFrameDropDown, but the reference to 'this' was not a reference to object IFrameDropDown.

Try the following code (not tested!!), and see if it will act as you expect:

[CODE]function IFrameDropDown () {
// object property initializations
// make them properties--with 'this'---only if you want them to be 'public'
// make them variables --with 'var'---if you want them to be 'private', as in a class
this.topLeftX = "0px";
this.topLeftY = "0px";
this.width = "100px";
this.height = "200px";
this.url = "mvm.php";
this.inputField = Object; // maybe new Object(); ??
// now your methods for the object
this.init = function() {
this.inputField = document.getElementById("whiteplayer");
this.addEvents(this.inputField, "change");
};
this.addEvents = function(obj, e) {
if (window.addEventListener) { // Mozilla, Netscape, Firefox
obj.addEventListener(e, this.alerter, false);
} else { // IE
obj.attachEvent("on" + e, this.alerter);
}
};
this.alerter = function() {
alert("sdf");
};
this.setTopLeftX = function(x) {
this.topLeftX = x + "px";
};
this.setTopLeftY = function(y) {
this.topLeftY = y + "px";
};
}
var frameDropDownObj = new IFrameDropDown();
window.onload = frameDropDownObj.init;
[/CODE]


You see, you basically have IFrameDropDown as an associative array created in global scope (not the kind of object you seek). Even as var in function scope, 'this' would refer to the global/window object. Only by calling the function as a constructor (requiring use of the 'new' keyword) would your scope be altered such that 'this' referred to the object (being) constructed.

But if you want to create multiple instances of the "interface," the constructor above should work for you.
Copy linkTweet thisAlerts:
@kukipeiauthorAug 29.2009 — Thank you, mavigozler

you gave me right direction.

I just added this code:
[CODE]function pageLoader() {
IFrameDropDown.init();
}

window.onload = pageLoader;[/CODE]


instead of
[CODE]window.onload = IFrameDropDown.init;[/CODE]
×

Success!

Help @kukipei spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...