I am a completely newbie to javascript. In the below template and JS function I am trying to display person1 name and person2 name and displaying whether they are equal.
<!DOCTYPE html>
<html>
<head>
<script>
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(){
alert("in function" + this.name);
};
}
var person1 = new Person("Nicholas", 29, "Software Engineer");
var person2 = new Person("Greg", 27, "Doctor");
alert(person1.sayName());
alert(person2.sayName());
alert(person1.sayName() == person2.sayName() );
</script>
</head>
<body>
<div>
</div>
</body>
</html>
But i am getting these alerts one by one.
in functionNicholas
undefined
in functionGreg
undefined
in functionNicholas
in functionGreg
true
Please help me with the control flow. Pardon me if the question is too silly and basic. Thanks
I don't know why i am getting undefined alerts twice.
It alerts undefined
because, alert
doesn't return anything, or rather your function ie person1.sayName()
doesn't return anything, it just alerts.
According to your naming convention sayName
is correct because it does its job by alerting , printing, displaying or similar, but doesn't have to return anything, and it just doesn't return anything. and hence person1.sayName() == person2.sayName()
is true because undefined == undefined
.
When you do:
alert(person1.sayName());
The following happens:
person1.sayName()
. alert('in function' + this.name);
, which causes in functionNicholas
to be alerted. sayName
function returns. Since there was no return somevalue
statement, it returns undefined
. alert
with the value that was returned, so it's like alert(undefined)
, which is why you see undefined
alerted. WHen you do:
alert(person1.sayName() == person2.sayName());
it calls each person's sayName
method, which alerts their name. They each return undefined
, so you do:
alert (undefined == undefined);
Since they're equal, this alerts true
.
You should use:
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.sayName = function(){
alert("in function" + this.name);
};
}
var person1 = new Person("Nicholas", 29, "Software Engineer");
var person2 = new Person("Greg", 27, "Doctor");
person1.sayName();
person2.sayName();
sayName() function doesn't return any value, the default return value is undefined so what u are actually doing is compare one undefined to another undefined
var x = person1.sayName()
// x is undefined
u can alter sayName() to return the current name after the alert:
this.sayName = function(){
alert("in function" + this.name);
return this.name;
};
Each time you run your sayName() function, you are creating an alert in the function and then returning undefined. Something is ALWAYS returned in JavaScript, if nothing is specified then it is undefined.
You first print sayName for Nicholas, which alerts his name, then returns undefined. You then alert the returned value (undefined).
Repeat for Greg.
The last line compares the two, which first alerts both of their names and then does the comparison of the two returned functions (in other words, undefined == undefined which is TRUE). True is then alerted.
The other answers are correct. Also, for the comparison, in your last alert, I suppose you wanted to check if the persons' names were the same.
alert(person1.name === person2.name);
For that, you can access each objects' name
property using the "dot syntax," above. The ===
(triple-equals) operator is the best operator for comparisons in this language. You should prefer it to the standard [unreliable] ==
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.