/    Sign up×
Community /Pin to ProfileBookmark

Define a simplest class

[CODE]function MyClass()
{
[B][COLOR=”Red”]this[/COLOR][/B].setPanStyleByName = function(idName,t,r,b,l,i100)
{
var o=window.document.getElementById(idName);
[B][COLOR=”Red”]this[/COLOR][/B].setPanStyleByNode(o,t,r,b,l,i100);
}

[B][COLOR=”Red”]this[/COLOR][/B].setPanStyleByNode = function(o,t,r,b,l,i100)
{

}
}[/CODE]

The class includes 2 functions, first function calls second function.
Anyway, the class has error and doesn’t work.

What is wrong?

Thanks.

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@rtretheweyMay 11.2012 — You define the object's functions in the wrong order. When you define setPanStyleByName(), it refers to setPanStyleByNode(), which doesn't exist yet.
Copy linkTweet thisAlerts:
@Jeff_MottMay 11.2012 — The code looks fine to me. The problem is probably somewhere in all the code you haven't shown us.
Copy linkTweet thisAlerts:
@KorMay 11.2012 — You judge JavaScript as it would be a class based language, which is not.

You have there is a [I]constructor[/I], 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 [B]this[/B] to have different meanings, according to the scope. And there is a difference between the function declaration and the function expression.

Try this:
<i>
</i>function MyClass()
{
this.setPanStyleByName = function(idName,t,r,b,l,i100)
{
var o=window.document.getElementById(idName);
[COLOR="Blue"]setPanStyleByNode(o,t,r,b,l,i100);[/COLOR]
}

<i> </i>this.setPanStyleByNode = function(o,t,r,b,l,i100)
<i> </i>{
<i> </i> ...
<i> </i>}
[COLOR="Blue"]var setPanStyleByNode=this.setPanStyleByNode;[/COLOR]
}
Copy linkTweet thisAlerts:
@Jeff_MottMay 11.2012 — Try this:
<i>
</i> [COLOR="Blue"]setPanStyleByNode(o,t,r,b,l,i100);[/COLOR]
[/QUOTE]


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.
  • Copy linkTweet thisAlerts:
    @KorMay 11.2012 — 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.
  • [/QUOTE]

    No, it is not global as long as it is defined[I] inside[/I] the constructor. have you noticed?
    <i>
    </i>function MyClass()
    {
    [B][COLOR="Blue"]var[/COLOR][/B] setPanStyleByNode=this.setPanStyleByNode;
    }

    Probably I should have avoided the confusion if I would write like:
    <i>
    </i>function MyClass()
    {
    this.setPanStyleByName = function(idName,t,r,b,l,i100)
    {
    var o=window.document.getElementById(idName);
    [COLOR="Blue"]foo[/COLOR](o,t,r,b,l,i100);
    }

    this.setPanStyleByNode = function(o,t,r,b,l,i100)
    {
    ...
    }
    [COLOR="Blue"]var foo=this.setPanStyleByNode;[/COLOR]
    }
    Copy linkTweet thisAlerts:
    @Jeff_MottMay 11.2012 — A simple test:

    <i>
    </i>function Test()
    {
    this.method1 = function()
    {
    foo();
    }

    <i> </i>this.method2 = function()
    <i> </i>{
    <i> </i> console.log(this); // DOMWindow
    <i> </i>}

    <i> </i>var foo = this.method2;
    }

    var t = new Test();
    t.method1();


    The way you changed the code will cause method2 to be invoked incorrectly.
    Copy linkTweet thisAlerts:
    @KorMay 11.2012 — A simple test:
    <i>
    </i>function myClass(){
    this.method2=function(arg){
    arg++;
    method1(arg);
    }
    this.method1=function(arg){
    alert(arg)
    }
    var method1=this.method1;
    }
    var myObject=new myClass();
    myObject.method1(1);
    myObject.method2(1);
    Copy linkTweet thisAlerts:
    @Jeff_MottMay 11.2012 — Neither of those methods use the "this" value. All you did was avoid revealing the mistake.
    Copy linkTweet thisAlerts:
    @KorMay 11.2012 — Neither of those methods use the "this" value. All you did was avoid revealing the mistake.[/QUOTE]
    Nonsense. Why should they use the token [B]this[/B] 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?
    Copy linkTweet thisAlerts:
    @Jeff_MottMay 11.2012 — Nonsense. Why should they use the token [B]this[/B] by all means?[/QUOTE]

    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.


    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?[/QUOTE]

    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.
    Copy linkTweet thisAlerts:
    @svidgenMay 11.2012 — This thread has taken a bad turn. Starting from the top, this response is perfect:

    The code looks fine to me. The problem is probably somewhere in all the code you haven't shown us.[/QUOTE]

    There's nothing wrong with the originally posted code, except for the placeholders and lack of context.

    Some error message text would be helpful too ...
    Copy linkTweet thisAlerts:
    @html20009876authorMay 11.2012 — [B]Thanks for all of above[/B],

    I figure it out: problem is at other places, not inside the class.

    :p:p:p:p:p:p:p:p:p:p


    .
    ×

    Success!

    Help @html20009876 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 6.17,
    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: @nearjob,
    tipped: article
    amount: 1000 SATS,

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

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...