English 中文(简体)
如何使用一种并非一种功能的等级方法?
原标题:How to do a class method that is not a function?
class Library {
    constructor(a){
    this.total = a;
    }
            
        add(...a){
            for (const arg of a) {
                  this.total += arg;
            }
            return this;
        }
        
        subtract(...b){
            for (const args of b) {
              this.total -= b;
            }
            return this;
            }
            
   
        result() {
            console.log(this.total);
            return this.total
            }
            
       
            


}

    $ = new Library(10); // inititliaize the base as 10

// var x = $.add(3,5).add(5,10).subtract(10).result;    
// x should be 10+3+5+5+10-10 or basically 23
// var y = $.add(3,5).add(5,10).subtract(10,3,5).result to return 15

问题需要确切的思路。

var x = $.add(3,5).add(5,10).subtract(10).result; 

问题要求将一种并非我所尝试的一种功能的“结果”方法链起来。

  result = function(){
    return this.total;
  };

是否有办法在不使用职能的情况下将某一类别的价值返还?

问题回答

我不敢肯定,为什么你试图使这种情况复杂化,因为显而易见的答案是利用一个称为结果的公共阶层。

class Library {
    result;
    
    constructor(a) {
        this.result = a;
    }
         
    add(...a){
        for (const arg of a) {
          this.result += arg;
        }
        return this;
    }  
    subtract(...b){
        for (const args of b) {
           this.result -= b;
        }
        return this;
    }
}

$ = new Library(0);
var x = $.add(3,5).add(5,10).subtract(10).result;
console.log(x);

当时有“维护财产”的矫正器,这确实有助于避免称职,只是收回财产的价值。

在这种情况下,你们需要使用get,该<>将物体财产与在研究该财产时将要求的功能联系起来。

get result() {
  return this.total
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签