Web Analytics Made Easy -
StatCounter Custom Object (inheritance?) - CodingForum

Announcement

Collapse
No announcement yet.

Custom Object (inheritance?)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom Object (inheritance?)

    Hi.

    Code:
    function one(n) {
    	this.n = n ;
    	this.list = new two() ;
    	this.list() ;
    	}
    
    function two() {
    	alert(this.n) ;
    	}
    
    hi = new one(50) ;
    How do I make two able to access all of one's properties/methods?
    Regards
    VKiDV

  • #2
    A good way is to create a method for the object you have created, this is shown below

    Code:
    function one(n) { // create the object
    	this.n = n ;
    }
    
    one.prototype.two = function() { // create a new method for the one object
    	alert(this.n);
    }
    
    var hi = new one(50) ; // create an instance of the object
    
    hi.two(); // this would alert 50
    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

    Comment


    • #3
      This is the only way I know to achieve something like "inheritance" in JavaScript:
      Code:
      function one(n) {
      	this.n = n;
      }
      function two() {
      	this.getN = function() { return this.n; }
      	this.setN = function(n) { this.n = n; }
      }
      one.prototype = new two();
      hi = new one(50);
      alert(hi.getN());
      hi.setN(100);
      alert(hi.getN());
      dumpfi
      "Failure is not an option. It comes bundled with the software."
      ....../)/)..(\__/).(\(\................../)_/)......
      .....(-.-).(='.'=).(-.-)................(o.O)...../<)
      ....(.).(.)("}_("}(.)(.)...............(.)_(.))¯/.
      ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
      Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

      Comment


      • #4
        Code:
        function one(n) {
            this.n = n;
        }
        
        
        function two() {
            alert(this.n);
        }
        
        
        var oOne = new one(5);
        two.call(oOne);
        jasonkarldavis.com

        Comment


        • #5
          http://crockford.com/javascript/inheritance.html
          Glenn
          vBulletin Mods That Rock!

          Comment


          • #6
            Thanks for the replies everyone.

            I think I've cracked it, I don't think it's inheritance but it works as I wanted it to.

            Code:
            function Animal( Name, Age ) {
            	this.Name = Name ;
            	this.Age = Age ;
            	this.Statistics = new Statistics(this) ;
            	this.Statistics.Calculate() ;
            	}
            
            function Statistics( Parent ) {
            	this.Parent = Parent ;
            	this.Calculate = Calculate ;
            	}
            
            function Calculate() {
            this.Weight = this.Parent.Age * 2 ;
            }
            
            var myFish = new Animal("Geoffrey",.5) ;
            alert(myFish.Statistics.Weight) ;
            EDIT: Before anybody asks, this isn't my real code =P heh. Its just a made up example, so I don't know why Weight is twice the age.
            Does 'this' copy the whole object or is it just a reference/pointer?

            Is there any more elegant/more efficient ways to do the above?

            Thanks for the link glenn, and interesting code from dumpfi, Narg and jkd. I think the original problem was, I didn't know what I wanted, I wasn't sure if it was inheritance or not.
            Last edited by vkidv; Aug 3, 2005, 02:58 PM.
            Regards
            VKiDV

            Comment

            Working...
            X