problema con inheritance

  • Creatore Discussione Creatore Discussione shivi
  • Data di inizio Data di inizio

shivi

Nuovo Utente
14 Feb 2012
25
0
0
Codice:
function objTest(){
    
          function myObj(){
                    this.name = "";
                    this.age = "";
                }
            
                myObj.prototype.setName = function(newName){
                        if(typeof newName != "undefined"){this.name = newName;}else{document.write("enter a valid name<br>")}
                }
            
                myObj.prototype.setAge = function(newAge){
                    this.age = newAge;
                }
            
                myObj.prototype.getName = function(){
                    return this.name;
                }
            
                myObj.prototype.getAge = function(){
                    return this.age;
                }
            
                var personInfo = new myObj();
                    personInfo.setName("shivi");
                    personInfo.setAge(28);
                    document.write("Your name is " + personInfo.getName() + " and your age is " + personInfo.getAge());
                
                function girlName(){
                    myObj.call(this);
                    this.girlmode = "sexy";
                }
            
            
                girlName.prototype = new myObj();
                girlName.prototype.constructor = girl;
                
                girl.prototype.setGirlMode = function(girlmode){
                    this.girlmode = girlmode;
                }
            
                girl.prototype.getGirlMode = function(){
                    return this.girlmode;
                }
            
                var getnewgirlMode = new girl;
                getnewgirlname.setGirlMode("hot");
                document.write("girl mode is  " + getnewgirlMode.getGirlMode());


}

ma non mi visualizza nulla come output perchè
 
Direi che è tutto un po' troppo confuso e complicato. Prova così:
Codice:
function Person()
{
    this.name = '';
    this.age  = 0;

    this.setName = function(name) {
        this.name = name;
    }

    this.getName = function() {
        return this.name;
    }

    this.setAge = function(age) {
        this.age = age;
    }

    this.getAge = function() {
        return this.age;
    }
}

Girl.prototype = new Person();

function Girl()
{
    this.mode = 'sexy';

    this.setMode = function(mode) {
        this.mode = mode;
    }

    this.getMode = function() {
        return this.mode;
    }
}
 

Discussioni simili