我一直在寻找关于自我调用函数的信息,在某个地方我偶然发现了这个符号:
+function(){}
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释一下+这个功能前面的符号意味着什么?
我似乎无法找到在javascript中重载[]运算符的方法.有人知道吗?
我在想......
MyClass.operator.lookup(index)
{
return myArray[index];
}
Run Code Online (Sandbox Code Playgroud)
或者我不是在看正确的事情.
考虑到这个JavaScript"类"定义,这是我能想到的最好的方法来解决这个问题:
var Quota = function(hours, minutes, seconds){
if (arguments.length === 3) {
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.totalMilliseconds = Math.floor((hours * 3600000)) + Math.floor((minutes * 60000)) + Math.floor((seconds * 1000));
}
else if (arguments.length === 1) {
this.totalMilliseconds = hours;
this.hours = Math.floor(this.totalMilliseconds / 3600000);
this.minutes = Math.floor((this.totalMilliseconds % 3600000) / 60000);
this.seconds = Math.floor(((this.totalMilliseconds % 3600000) % 60000) / 1000);
}
this.padL = function(val){
return (val.toString().length === 1) ? "0" + val : val;
}; …Run Code Online (Sandbox Code Playgroud) 是否可以在JavaScript中的类型实例之间定义自定义运算符?
例如,假设我有自定义矢量类,是否可以使用
vect1 == vect2
Run Code Online (Sandbox Code Playgroud)
检查是否相等,而底层代码是这样的?
operator ==(a, b) {
return a.x == b.x && a.y == b.y && a.z == b.z;
}
Run Code Online (Sandbox Code Playgroud)
(当然这是无稽之谈.)