SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Altering this?

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Altering this?

    Code:
    function func1() {
    
    this.val = 1
    } function func2() {
    this.getVal = function() {
    return this.val
    }
    } var inst1 = new func1(); var inst2 = new func2(); inst2.getVal(); //should return 1
    So the point is how can i change the object that this refers to?
    NOTELAY.COM - Have a nice noty day
    ETA CONSULTING

  2. #2
    SitePoint Enthusiast Moridin_8's Avatar
    Join Date
    Apr 2007
    Location
    UK, soon in USA
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    you are playing with object instances. Therefore what you are attempting will not work. Below are a few possibilities for you. Hope this is what you are asking for.

    Code:
    function shared()
    {
    	this.shared = 100
    }
    
    function func1() 
    {
    	this.val = 1
    	this.getVal = blah;
    	
    	this.getVal2 = blah2;
    }
    
    function func2() 
    {
    	this.getVal = blah;
    	
    	this.getVal2 = blah2;
    	
    	this.getVal3 = function(){return inst1.getVal()};
    }
    
    function blah() 
    {
    	return this.val;
    }
    function blah2() 
    {
    	return instShared.shared;
    }
    
    var instShared = new shared();
    var inst1 = new func1();
    var inst2 = new func2();
    
    document.writeln(inst1.getVal());  //should return 1
    document.write("<br />");
    document.writeln(inst2.getVal());  //should return 'undefined'
    
    document.write("<br />");
    
    document.writeln(inst1.getVal2());  //should return 100
    document.write("<br />");
    document.writeln(inst2.getVal2());  //should return 100
    
    document.write("<br />");
    document.writeln(inst2.getVal3());  //should return 1
    Matt "Moridin8" Warren
    ======================================
    Website: www.csharp-architect.com
    ....IRC: Usually freenode... '##csharp'

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well the point is i dont want to use inst1.getVal() in the func2 because i dont know the name of the first instance, never mind i found a js framework called mootools that allows doind this by typing for example

    inst2.func = function(args) {
    //expression
    }.bind(inst1);

    they made it with a lot of code i thought it's possible to make it with less with some prototype, constructors ... etc etc. im not so deep in to js.
    NOTELAY.COM - Have a nice noty day
    ETA CONSULTING

  4. #4
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    should that be

    Code:
    function func2() {
        var obj = this;
        this.getVal = function() {
    
            return obj.val
    
        } 
    
    }

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you basically want inheritance:

    Code:
    function Car() {
    	this.amountOfWheels = 4;
    }
    
    function Volvo() {
    	Car.call(this);
    }
    
    Volvo.prototype = new Car();
    
    Volvo.prototype.getWheels = function() {
        alert(this.amountOfWheels);
    }
    
    var myCar = new Volvo();
    
    alert(myCar.amountOfWheels);
    
    // or use method
    myCar.getWheels();
    Last edited by Pepejeria; Apr 12, 2007 at 00:00.


Bookmarks

Posting Permissions

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