javascript - Undefined Object method -
i have code , use display
method keeps giving me:
url undefined
name undefined
description undefined
i don't know why i'm getting errors though providing proprieties. can please identify problem me?
function website(name,url,description) { //proparties this.name=name; this.url=url; this.description=description; // methods this.getname=getname; this.geturl=geturl; this.getdescription=getdescription; this.display=display; // getname method function getname(name) { this.getname=name; } // geturl method function geturl(url){ this.geturl=url; } // getdescription function getdescription(description){ this.getdescription=description; } function display(name,url,description){ alert("url :" +url +" name :"+name+" description is: "+description); } } // set object proparites web=new website("mywebsite","http://www.mywebsite.com","my nice website"); // call methods var name = web.getname("mywebsite"); var url = web.geturl("http://www.mywebsite.com"); var description = web.getdescription("my nice website"); web.display(name,url,description);
your getter functions setters overwrite (?). change them to
function getname(){ return this.name; } function geturl(){ return this.url; } function getdescription(){ return this.description; }
and
function setname(name){ this.name = name; } function seturl(url){ this.url = url; } function setdescription(description){ this.description = description; }
if want setters return set value, add return
keywords before assignments.