www.webdeveloper.com
Results 1 to 13 of 13

Thread: Define a simplest class

  1. #1
    Join Date
    Apr 2005
    Posts
    210

    Define a simplest class

    Code:
    function MyClass()
    {
    	this.setPanStyleByName = function(idName,t,r,b,l,i100)
    	{
    		var o=window.document.getElementById(idName);
    		this.setPanStyleByNode(o,t,r,b,l,i100);
    	}
    
    	this.setPanStyleByNode = function(o,t,r,b,l,i100)
    	{
                ...
    	}
    }
    The class includes 2 functions, first function calls second function.
    Anyway, the class has error and doesn't work.

    What is wrong?

    Thanks.

  2. #2
    Join Date
    Mar 2011
    Posts
    1,148
    You define the object's functions in the wrong order. When you define setPanStyleByName(), it refers to setPanStyleByNode(), which doesn't exist yet.

  3. #3
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    The code looks fine to me. The problem is probably somewhere in all the code you haven't shown us.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    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;
    }
    Last edited by Kor; 05-11-2012 at 09:56 AM.

  5. #5
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Quote Originally Posted by Kor View Post
    Try this:
    Code:
    		setPanStyleByNode(o,t,r,b,l,i100);
    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.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Jeff Mott View Post
    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;
    }

  7. #7
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    A simple test:

    Code:
    function Test()
    {
        this.method1 = function()
        {
            foo();
        }
    
        this.method2 = function()
        {
            console.log(this); // DOMWindow
        }
    
        var foo = this.method2;
    }
    
    var t = new Test();
    t.method1();
    The way you changed the code will cause method2 to be invoked incorrectly.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    A simple test:
    Code:
    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);

  9. #9
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Neither of those methods use the "this" value. All you did was avoid revealing the mistake.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  10. #10
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Jeff Mott View Post
    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?

  11. #11
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Quote Originally Posted by Kor View Post
    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.


    Quote Originally Posted by Kor View Post
    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.
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  12. #12
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    This thread has taken a bad turn. Starting from the top, this response is perfect:

    Quote Originally Posted by Jeff Mott View Post
    The code looks fine to me. The problem is probably somewhere in all the code you haven't shown us.
    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 ...

  13. #13
    Join Date
    Apr 2005
    Posts
    210
    Thanks for all of above,

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




    .

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles