Reputation: 301
I have this code and as i use the display
method it keeps giving me:
url is undefined
name is undefined
description is undefined
I don't know why i'm getting errors even though I am providing it will all the proprieties. Can somebody please identify the problem for 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 is :" +url +" Name Is :"+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);
Upvotes: 0
Views: 123
Reputation: 1
You should made return for the value as the following on each method:
// GetName Method
function getName() {
return this.getName = name;
}
// GetUrl Method
function getUrl() {
return this.getUrl = url;
}
// GetDescription Method
function getDescription() {
return this.getDescription = description;
}
Upvotes: 0
Reputation: 664195
Your getter functions are setters that overwrite themselves (?). 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 you want your setters to return the set value, add return
keywords before the assignments.
Upvotes: 1
Reputation: 9368
Your getters should return a value instead of reassigning the getter itself e.g.
function getName() {
return this.name;
}
Upvotes: 1
Reputation: 686
I think you're pretty confused about how functions work. In your code you have:
this.getName=getName; // this sets a "getName" method on the "this" object
// to be some function that will be implemented below
function getName(name) // i believe this function shouldn't have any parameter...
{
this.getName=name; //now, you're overriding the "getName" that you set above,
// to be something completely different: the parameter you sent when calling this function!
// instead, you should do:
return name;
}
Upvotes: 2
Reputation: 632
I should declare it as
function () {
//property
this.name
//method
this.setName = function ( name ) {
this.name = name
}
}
They way your implementing it, asks for context problems
Upvotes: 1
Reputation: 26930
You wanted to write this? :
function setName(name)
{
this.name=name;
}
As I understood, you are setting, not getting properties. So:
var name = web.setName("mywebsite");
Upvotes: 1