Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Accessing a value from a "parrent" class

    Given this piece of code, is there a way for "child" to get the value 3.14

    Code:
    function parentClass() {
    	this.CONSTANT=3.14;
    };
    
    parentClass.child=function() {
    	alert(parentClass.CONSTANT); //how to make it get 3.14
    };
    
    x = new parentClass.child(1);
    Thanks

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by ken_shoti View Post
    Given this piece of code, is there a way for "child" to get the value 3.14

    Code:
    function parentClass() {
    	this.CONSTANT=3.14;
    };
    
    parentClass.child=function() {
    	alert(parentClass.CONSTANT); //how to make it get 3.14
    };
    
    x = new parentClass.child(1);
    Thanks
    Code:
    function A(){
      this.PI = 3.14;
    };
    
    function B(){};
    B.prototype = new A();
    
    var x = new B();
    alert(x.PI);
    http://www.javascriptkit.com/javatutors/oopjs.shtml
    http://www.javascriptkit.com/javatutors/object.shtml
    http://www.javascriptkit.com/javatutors/proto.shtml
    http://www.webreference.com/js/column34/
    http://www.webreference.com/js/column80/index.html
    http://www.webreference.com/js/column79/
    http://www.webreference.com/programm...g25/index.html

    best regards

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by ken_shoti View Post
    Given this piece of code, is there a way for "child" to get the value 3.14

    Thanks

    Code:
    function parentClass() {
    	this.CONSTANT=3.14;
    };
    
    parentClass.prototype.child=function() {
    	alert(this.CONSTANT); //how to make it get 3.14
    };
    
    x = (new parentClass).child(1);
    you were close, changes in red.
    Last edited by rnd me; 05-08-2009 at 04:03 AM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #4
    Regular Coder
    Join Date
    Nov 2005
    Posts
    230
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the corrections


  •  

    Posting Permissions

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