我尝试在JS中使用更多的方法,为模型类创建POJO / POCO。
我有一个Person对象:
function person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
return this;
}
现在我可以创建一些数据:
var personalities = new Array(new person("Alicia", 152, "F"),
new person("Bartolomeu Simpson", 36, "M"),
new person("Ayrton Senna", 58, "M"),
new person("Jean J. Michel", 36, "M"),
new person("Jean J. Michel", 37, "M"));
好吧,如果我调用sort方法就没有动作来订购这些数据:
personalities.sort();
for(var i = 0; i < personalities.length; i++)
console.log(personalities[i].name);
输出是:
Alicia
Bartolomeu Simpson
Ayrton Senna
Jean J. Michel
Jean J. Michel
我创建了一个toString()方法:
function person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
this.toString = function() {
return this.name + " " +
this.age + " years old" +
(this.gender == 'M' ? " man" : " woman");
};
return this;
}
现在排序方法是&#34; ok&#34;,输出现在是:
Alicia
Ayrton Senna
Bartolomeu Simpson
Jean J. Michel
Jean J. Michel
但我想做一些不同的事情,按名称和年龄排序(最早的)。我实现了方法compareTo(obj):
function person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
this.toString = function() {
return this.name + " " +
this.age + " years old" +
(this.gender == 'M' ? " man" : " woman");
};
this.compareTo = function(otherPerson) {
var resultOfComparison = 0;
if(this.name > otherPerson.name) {
resultOfComparison = 1;
return resultOfComparison;
}
else if(this.name < otherPerson.name) {
resultOfComparison = -1;
return resultOfComparison;
}
else if(this.age > otherPerson.age) {
resultOfComparison = -1;
return resultOfComparison;
}
else if(this.age < otherPerson.age) {
resultOfComparison = 1;
return resultOfComparison;
}
return resultOfComparison;
};
return this;
}
方法还可以,看看:
var jean1 = new person("Jean J. Michel", 36, "M");
var jean2 = new person("Jean J. Michel", 37, "M");
console.log(jean1.compareTo(jean2)); //1
console.log(jean1.compareTo(jean1)); //0
console.log(jean2.compareTo(jean1)); //-1
我可以使用这种方法对数组进行排序吗?像这样:
personalities.sort(person.compareTo);
我真的不想在使用我的模型的服务类中这样做:
personalities.sort(function(personA, personB) {
var resultOfComparison = 0;
if(personA.name > personB.name) {
resultOfComparison = 1;
return resultOfComparison;
}
else if(personA.name < personB.name) {
resultOfComparison = -1;
return resultOfComparison;
}
else if(personA.age > personB.age) {
resultOfComparison = -1;
return resultOfComparison;
}
else if(personA.age < personB.age) {
resultOfComparison = 1;
return resultOfComparison;
}
return resultOfComparison;
});
我认为这是一种模范责任。
感谢您的帮助。
答案 0 :(得分:2)
嗯,传递给sort()
的比较器函数需要带两个参数,因此它不能成为实例方法。我建议你把它定义为&#34;静态&#34;方法:
function person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
person.compare = function(personA, personB) {
if(personA.name > personB.name) return 1;
if(personA.name < personB.name) return -1;
return personB.age - personA.age;
}
然后你可以在排序时调用它:
personalities.sort(person.compare);
我希望我能正确理解你的要求。
答案 1 :(得分:0)
非常感谢所有回复。 Máté给了我一个我在JS中真正不知道的主题的路径。
我的最后一堂课是:
function person(name, age, gender) {
this.name = name;
this.age = age;
this.gender = gender;
this.toString = function() {
return this.name + " " +
this.age + " years old" +
(this.gender == 'M' ? " man" : " woman");
};
return this;
}
我的compareTo方法现在是静态的:
person.compareTo = function(personA, personB) {
var resultOfComparison = 0;
if(personA.name > personB.name) {
resultOfComparison = 1;
return resultOfComparison;
}
else if(personA.name < personB.name) {
resultOfComparison = -1;
return resultOfComparison;
}
else if(personA.age > personB.age) {
resultOfComparison = -1;
return resultOfComparison;
}
else if(personA.age < personB.age) {
resultOfComparison = 1;
return resultOfComparison;
}
return resultOfComparison;
};
所有这些代码都在PersonModel.js中。
现在我的服务/ DAO我可以这样做:
var personalities = new Array(new person("Bartolomeu Simpson", 36, "M"),
new person("Ayrton Senna", 58, "M"),
new person("Jean J. Michel", 36, "M"),
new person("Jean J. Michel", 37, "M"),
new person("Alicia", 152, "F"));
personalities.sort(person.compareTo);
结果将是:
Alicia 152 years old woman
Ayrton Senna 58 years old man
Bartolomeu Simpson 36 years old man
Jean J. Michel 37 years old man
Jean J. Michel 36 years old man
干得好,关闭案例:)