Javascript将getter设置为类本身
我已经找到了很多代码来在类中生成getter和/或setter,但没有一个代码是完整的。 我的目的是能够做比较(Javascript将getter设置为类本身,javascript,getter,Javascript,Getter,我已经找到了很多代码来在类中生成getter和/或setter,但没有一个代码是完整的。 我的目的是能够做比较(newtime()>myTime)等事情 如果需要自定义比较,可以实现。当使用、+etc.运算符时,将对象强制为基元时使用此选项 范例 var Time = function (x) { this.x = x; }; Time.prototype.valueOf = function () { console.log("valueOf() called"); r
newtime()>myTime
)等事情
如果需要自定义比较,可以实现。当使用、+etc.运算符时,将对象强制为基元时使用此选项 范例
var Time = function (x) {
this.x = x;
};
Time.prototype.valueOf = function () {
console.log("valueOf() called");
return this.x;
}
var a = new Time(1);
var b = new Time(2);
console.log(a > b); // false
console.log(a < b); // true
console.log(a + b); // 3
console.log(a << 10); // 1024
console.log(a / b); // 0.5
var时间=函数(x){
这个.x=x;
};
Time.prototype.valueOf=函数(){
log(“valueOf()被调用”);
归还这个.x;
}
var a=新时间(1);
var b=新时间(2);
console.log(a>b);//假的
console.log(alog(a如果需要自定义比较,可以实现。当使用、+etc.运算符时,将对象强制为原语时使用此选项
范例
var Time = function (x) {
this.x = x;
};
Time.prototype.valueOf = function () {
console.log("valueOf() called");
return this.x;
}
var a = new Time(1);
var b = new Time(2);
console.log(a > b); // false
console.log(a < b); // true
console.log(a + b); // 3
console.log(a << 10); // 1024
console.log(a / b); // 0.5
var时间=函数(x){
这个.x=x;
};
Time.prototype.valueOf=函数(){
log(“valueOf()被调用”);
归还这个.x;
}
var a=新时间(1);
var b=新时间(2);
console.log(a>b);//false
console.log(alog(a让我们假设您需要让对象有一个返回总毫秒数的值,您可以执行以下操作:
function Time(hours, minutes, seconds, milliseconds) {
this.hours = hours;
this.minutes = minutes || 0;
this.seconds = seconds || 0;
this.milliseconds = milliseconds || 0;
}
Time.prototype.valueOf = function() {
return this.milliseconds +
this.seconds * 1000 +
this.minutes * 1000 * 60 +
this.hours * 1000 * 60 * 60;
};
然后,要比较时间,您可以执行以下操作:
var time1 = new Time(22,58),
time2 = new Time(23,59);
if (time1 < time2) {
console.log ("Yes, 22:58 is before 23:59");
}
var time1=新时间(22,58),
时间2=新时间(23,59);
如果(时间1<时间2){
console.log(“是的,22:58在23:59之前”);
}
假设您需要让对象具有返回总毫秒数的值,您可以执行以下操作:
function Time(hours, minutes, seconds, milliseconds) {
this.hours = hours;
this.minutes = minutes || 0;
this.seconds = seconds || 0;
this.milliseconds = milliseconds || 0;
}
Time.prototype.valueOf = function() {
return this.milliseconds +
this.seconds * 1000 +
this.minutes * 1000 * 60 +
this.hours * 1000 * 60 * 60;
};
然后,要比较时间,您可以执行以下操作:
var time1 = new Time(22,58),
time2 = new Time(23,59);
if (time1 < time2) {
console.log ("Yes, 22:58 is before 23:59");
}
var time1=新时间(22,58),
时间2=新时间(23,59);
如果(时间1<时间2){
console.log(“是的,22:58在23:59之前”);
}
无法理解您的要求。看起来您想要实现valueOf
和toString
:,我认为JavaScript无法处理运算符重载,因此您将无法与
运算符进行比较。我的建议是在myTime()中创建一个方法
需要时间()的类
对象并在参数中进行比较。我可以建议吗?我不理解您的要求。看起来您想要实现valueOf
和toString
:,我认为JavaScript不会处理运算符重载,因此您将无法与
运算符进行比较。我的建议是创建一个me您的myTime()
类中的方法,它在参数中接受Time()
对象并在内部进行比较。我可以建议您吗?谢谢,很高兴知道。但是,Time=time1-new Time();?JavaScript不支持运算符重载(除非您碰巧想要返回一个数字),您必须使用减法(date)方法,即time=time1.subtract(new time())使其返回(new Date()).setHours(…).getTime()所以我可以做:new time(time1 new time())谢谢,很高兴知道。但是,time=time1-new time();?JavaScript不支持运算符重载(除非您碰巧想要返回一个数字),您必须有一个subtract(Date)方法,即time=time1.subtract(new time())使其返回(new Date()).setHours(…).getTime()以便执行:new time(time1 new time())