Javascript 面向对象编程值随条件变化
大家好,我今天开始学习JavaScript中的面向对象编程 我创建了一个名为Person的构造函数对象(如果我理解=>不要太难,我是初学者),它包含四个参数(姓名、年龄、颜色、性别): 然后我有了新的实例(精确地说是两个实例),它们调用了四个参数 最后,我用document.write()对每个实例的person对象的不同属性/值进行了一句访问,以如下方式输出结果:Javascript 面向对象编程值随条件变化,javascript,oop,if-statement,instance,Javascript,Oop,If Statement,Instance,大家好,我今天开始学习JavaScript中的面向对象编程 我创建了一个名为Person的构造函数对象(如果我理解=>不要太难,我是初学者),它包含四个参数(姓名、年龄、颜色、性别): 然后我有了新的实例(精确地说是两个实例),它们调用了四个参数 最后,我用document.write()对每个实例的person对象的不同属性/值进行了一句访问,以如下方式输出结果: document.write(`<h3>${person1.name} is a ${person1.sex}, he
document.write(`<h3>${person1.name} is a ${person1.sex}, he is:${person1.age} years old and his favorite color is the "${person1.favColor.toUpperCase()}"</h3>`);
document.write("<br>");
document.write(`<h3>${person2.name} is a ${person2.sex}, she is:${person2.age} years old and her favorite color is the "${person2.favColor.toUpperCase()}"</h3>`);
我不知道为什么条件不起作用,因为它只输出“w”而不是“woman”
以下是完整的代码:
function person(name, age, color,gender) {
this.name = name;
this.age = age;
this.favColor = color;
this.sex=gender
if(person.sex=="w"){
person.sex="woman"
}
else if(person.sex=="m"){
person.sex="man"
}
}
let person1 = new person("John", 42, "green","m");
let person2 = new person("Amy", 21, "red","w");
document.write(`<h3>${person1.name} is a ${person1.sex}, he is:${person1.age} years old and his favorite color is the "${person1.favColor.toUpperCase()}"</h3>`);
document.write("<br>");
document.write(`<h3>${person2.name} is a ${person2.sex}, she is:${person2.age} years old and her favorite color is the "${person2.favColor.toUpperCase()}"</h3>`);
职能人员(姓名、年龄、肤色、性别){
this.name=名称;
这个。年龄=年龄;
this.favColor=颜色;
这个。性=性别
如果(人性别=“w”){
person.sex=“女性”
}
else if(person.sex==“m”){
person.sex=“man”
}
}
let person1=新人(“约翰”,42,“格林”,“m”);
let person2=新人(“艾米”,21,“红色”,“w”);
write(`${person1.name}是一个${person1.sex},他是:${person1.age}岁,他最喜欢的颜色是“${person1.favColor.toUpperCase()}”`);
文件。写(“
”);
document.write(`${person2.name}是一个${person2.sex},她是:${person2.age}岁,她最喜欢的颜色是“${person2.favColor.toUpperCase()}”`);
这里是输出:
=============更改为:
function person(name, age, color, gender) {
this.name = name;
this.age = age;
this.favColor = color;
this.sex= gender;
if(this.sex ==="w"){
this.sex = "woman";
}
else if(this.sex === "m"){
this.sex = "man";
}
}
使用
this.sex
而不是person.sex
。谢谢兄弟,它现在正在工作。它是面向对象的编程(与面向类的编程相反),而不是面向对象:-p当我在构造函数中时,我必须用“this”关键字调用它来引用当前对象的属性,就是它?
if(person.sex=="w"){
person.sex="woman"
}
}
function person(name, age, color,gender) {
this.name = name;
this.age = age;
this.favColor = color;
this.sex=gender
if(person.sex=="w"){
person.sex="woman"
}
else if(person.sex=="m"){
person.sex="man"
}
}
let person1 = new person("John", 42, "green","m");
let person2 = new person("Amy", 21, "red","w");
document.write(`<h3>${person1.name} is a ${person1.sex}, he is:${person1.age} years old and his favorite color is the "${person1.favColor.toUpperCase()}"</h3>`);
document.write("<br>");
document.write(`<h3>${person2.name} is a ${person2.sex}, she is:${person2.age} years old and her favorite color is the "${person2.favColor.toUpperCase()}"</h3>`);
function person(name, age, color, gender) {
this.name = name;
this.age = age;
this.favColor = color;
this.sex= gender;
if(this.sex ==="w"){
this.sex = "woman";
}
else if(this.sex === "m"){
this.sex = "man";
}
}