gongpex Posted June 21, 2011 Share Posted June 21, 2011 hello everyone,I had tried to create object inside object like this script using "new" function <script language="javascript"> function person(name, age, gender) { this.name=name; this.age=age; this.gender=gender; } function car(factory, model, year, owner) { this.factory=factory; this.model=model; this.year=year; this.owner=owner; } lany= new person("lany",15,"female"); andy= new person("Andy",40,"male"); henry= new person("Henry",35,"male"); tiny= new person("Tiny",55,"female"); car_me=new car("toyota","corolla",1996,"lany"); car_dad=new car("nissan","pulsar",1994,"andy"); car_uncle=new car("daihatsu","charade",1992,"henry"); car_aunt=new car("honda","civic",1990,"tiny"); document.write("my car is=" , car_me.owner.age); </script> but the results is "undefined" what's mistake of this script?and what the function of "this." in javascript?please someone tell to methanks Link to comment Share on other sites More sharing options...
radish Posted June 21, 2011 Share Posted June 21, 2011 The purpose of "this." is to differentiate that objects variables from other variables in the script.it basically is telling the browser"this variable is inside this object use it."this quote is lifted from the w3c tutorial on objects. http://www.w3schools.com/js/js_objects.asp Inside the function you need to assign things to this.propertyName. The reason for all the "this" stuff is that you're going to have more than one person at a time (which person you're dealing with must be clear). That's what "this" is: the instance of the object at hand. lany= new person("lany",15,"female"); andy= new person("Andy",40,"male"); henry= new person("Henry",35,"male"); tiny= new person("Tiny",55,"female"); car_me=new car("toyota","corolla",1996,"lany"); car_dad=new car("nissan","pulsar",1994,"andy"); car_uncle=new car("daihatsu","charade",1992,"henry"); car_aunt=new car("honda","civic",1990,"tiny"); also you need to insert a var in front of your object variables to tell javascript that you are creating a new variable in that scope. otherwise it tries to look for a global variable.like this var lany= new person("lany",15,"female"); Link to comment Share on other sites More sharing options...
Ingolme Posted June 21, 2011 Share Posted June 21, 2011 The problem is that you're passing a string as the owner, rather than the variable refering to the owner object. lany= new person("lany",15,"female"); car_me=new car("toyota","corolla",1996,lany); // This is not quoted because it's not a string Link to comment Share on other sites More sharing options...
gongpex Posted June 22, 2011 Author Share Posted June 22, 2011 The problem is that you're passing a string as the owner, rather than the variable refering to the owner object. lany= new person("lany",15,"female"); car_me=new car("toyota","corolla",1996,lany); // This is not quoted because it's not a stringYeah, you right thank you for answer Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.