第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在JavaScript中重載算術(shù)運算符?

在JavaScript中重載算術(shù)運算符?

哈士奇WWW 2019-07-25 10:00:46
在JavaScript中重載算術(shù)運算符?考慮到這個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;     };     this.toString = function(){         return this.padL(this.hours) + ":" + this.padL(this.minutes) + ":" + this.padL(this.seconds);     };     this.valueOf = function(){         return this.totalMilliseconds;     };};以及以下測試設(shè)置代碼:var q1 = new Quota(23, 58, 50);var q2 = new Quota(0, 1, 0);var q3 = new Quota(0, 0, 10);console.log("Quota 01 is " + q1.toString());    // Prints "Quota 01 is 23:58:50"console.log("Quota 02 is " + q2.toString());    // Prints "Quota 02 is 00:01:00"console.log("Quota 03 is " + q3.toString());    // Prints "Quota 03 is 00:00:10"是否有隱式創(chuàng)建的任何方式q4為Quota使用加法運算符如下對象...var q4 = q1 + q2 + q3;console.log("Quota 04 is " + q4.toString());    // Prints "Quota 04 is 86400000"而不是訴諸......var q4 = new Quota(q1 + q2 + q3);console.log("Quota 04 is " + q4.toString());    // Prints "Quota 04 is 24:00:00"如果不是這個領(lǐng)域的最佳實踐建議是什么,可以通過算術(shù)運算符組合自定義數(shù)字JavaScript對象?
查看完整描述

3 回答

?
九州編程

TA貢獻1785條經(jīng)驗 獲得超4個贊

由于每個人都投了我的另一個答案,我想發(fā)布概念代碼的證明,其實際上按預(yù)期工作。

這已在chrome和IE中測試過。

//Operator Overloadingvar myClass = function () {//Privatesvar intValue = Number(0),
    stringValue = String('');//Publicsthis.valueOf = function () {
    if (this instanceof myClass) return intValue;
    return stringValue;}this.cast = function (type, call) {
    if (!type) return;
    if (!call) return type.bind(this);
    return call.bind(new type(this)).call(this);}}//Derived classvar anotherClass = function () {//Store the base referencethis.constructor = myClass.apply(this);var myString = 'Test',
    myInt = 1;this.valueOf = function () {
    if (this instanceof myClass) return myInt;
    return myString;}}//Testsvar test = new myClass(),anotherTest = new anotherClass(),composed = test + anotherTest,yaComposed = test.cast(Number, function () {
    return this + anotherTest}),yaCComposed = anotherTest.cast(Number, function () {
    return this + test;}),t = test.cast(anotherClass, function () {
    return this + anotherTest}),tt = anotherTest.cast(myClass, function () {
    return this + test;});debugger;

如果有人愿意提供技術(shù)解釋為什么這不夠好我會很高興聽到它!



查看完整回答
反對 回復(fù) 2019-07-25
?
犯罪嫌疑人X

TA貢獻2080條經(jīng)驗 獲得超4個贊

據(jù)我所知,Javascript(至少現(xiàn)在存在)不支持運算符重載。


我能建議的最好的方法是從其他幾個方法制作新的配額對象的類方法。這是我的意思的一個簡單例子:


// define an example "class"

var NumClass = function(value){

    this.value = value;

}

NumClass.prototype.toInteger = function(){

    return this.value;

}


// Add a static method that creates a new object from several others

NumClass.createFromObjects = function(){

    var newValue = 0;

    for (var i=0; i<arguments.length; i++){

        newValue += arguments[i].toInteger();

    }

    return new this(newValue)

}

并使用它像:


var n1 = new NumClass(1);

var n2 = new NumClass(2);

var n3 = new NumClass(3);


var combined = NumClass.createFromObjects(n1, n2, n3);


查看完整回答
反對 回復(fù) 2019-07-25
  • 3 回答
  • 0 關(guān)注
  • 837 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號