You judge JavaScript as it would be a class based language, which is not.
You have there is a constructor, not a class. That makes the inner functions to be the methods of the object, not the methods of the constructor.
That makes the token this to have different meanings, according to the scope. And there is a difference between the function declaration and the function expression.
Try this:
Code:
function MyClass()
{
this.setPanStyleByName = function(idName,t,r,b,l,i100)
{
var o=window.document.getElementById(idName);
setPanStyleByNode(o,t,r,b,l,i100);
}
this.setPanStyleByNode = function(o,t,r,b,l,i100)
{
...
}
var setPanStyleByNode=this.setPanStyleByNode;
}
I think that's actually a bad change. Now when setPanStyleByNode executes, its "this" value will be the global/window object, rather than the MyClass instance* object.
I think that's actually a bad change. Now when setPanStyleByNode executes, its "this" value will be the global/window object, rather than the MyClass instance* object.
* Using the term "instance" loosely.
No, it is not global as long as it is defined inside the constructor. have you noticed?
Code:
function MyClass()
{
var setPanStyleByNode=this.setPanStyleByNode;
}
Probably I should have avoided the confusion if I would write like:
Code:
function MyClass()
{
this.setPanStyleByName = function(idName,t,r,b,l,i100)
{
var o=window.document.getElementById(idName);
foo(o,t,r,b,l,i100);
}
this.setPanStyleByNode = function(o,t,r,b,l,i100)
{
...
}
var foo=this.setPanStyleByNode;
}
Neither of those methods use the "this" value. All you did was avoid revealing the mistake.
Nonsense. Why should they use the token this by all means? The OP wants to call a method of an object from within another method of the same object. There are more ways to do that. All I did was to provide what I consider the simplest one. What bothers you?
Nonsense. Why should they use the token this by all means?
Because that's how OOP works. The "this" object will be one of many possible instances. Once you call methods without it, then those methods can no longer access the instance that is being operated on.
Originally Posted by Kor
The OP wants to call a method of an object from within another method of the same object. There are more ways to do that. All I did was to provide what I consider the simplest one. What bothers you?
There's lots of code the OP hasn't shown us. It's very likely that his setPanStyleByNode method uses "this" in some way. But if he implements your suggestion, then "this" will be the wrong value.
In short, your suggestion did not solve the original problem, and will likely create new problems.
Bookmarks