/    Sign up×
Community /Pin to ProfileBookmark

Catching bug in javascript

Hello!
I am really new on js and i have one really important question.
If visual basic and firebug doesn’t show me any error and if code doesn’t work on any browser…so what should i do.
I did a little boring – to read my code again to find a simple syntax errors like – writte() for exemple.
But even that does not give me anything!
Is there any good recomendations how to change it all?

Look at this code and could some help to fix the problem:

<script language=”javascript” type=”text/javascript”>
function Animal() {
this.name = “No Name”;
}
Animal.prototype.setName = function(newName) {
if (typeof newName != “undefined”) {
this.name = newName;
}
else {
document.write(“Please enter a valid name”);
}
}
Animal.prototype.getName = function() {
return this.name;
}
dog.setName(“Spot”);

document.write(dog.setName() + “<br />”);

</script>
Browser gives me out only – Please enter a valid name??

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@twseitexAug 03.2011 — Hi,

f.e. Animal.prototype.setName = value


try

{Animal.prototype.setName = value}

catch (e)

{

// do something

}


use try-catch if you are not shure code at runtime create no errror.

Some properties have no default value (value after first set to value)

if(Animal.prototype.setName) maybe possible

if (typeof newName != "undefined") maybe possible



----------------------------------------


function Animal()

{

this.name = "No Name";

}

What ist this ? reference of Animal function ?


------------------------------------------


// +++++ step 1 check value and references

function AnimalCheckName(newName)

{

// if (typeof newName != "undefined")

// {this.name = newName;}

// else{document.write("Please enter a valid name");}

var boFlag=true;

// ceck params

if(newName!=null)

{if( .... check value from min to max ...) // f.e. != "" or (>=0 AND <=100)

{

// check this (if this exist and != reference of function)


...

return boFlag; // false if error only for CHECK

}


// +++++ step 2 set checked value to checked reference

function AnimalSetName(newName)

{

var boFlag=true;

// check

if(AnimalCheckName(newName)

{

// set value

try

{this.name = newName;} // whatever this is

catch(e)

{

.....

// set return

boFlag=false;

}

}

return boFlag; // false if error only for SET

}


if(AnimalSetName(newName)){alert("okay");}





-------------------------------------------------

document.write() create code to parse and

append code to document object


use confirm() or alert() etc. for messages with GUI.
×

Success!

Help @LittleBart spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...