7-13 WPS JS宏 this 用构造函数自定义类-2,调用内部对象必须用this
一、函数
this:
指 function Ceshi(){ } 过程名Ceshi ,在自定义类内部调用内部对象必须加this.
参数: this.num=99999
参数: this.Max=this.arr.reduce((x,y)=>(x>y)?x:y)
二、测试代码
function custom(Rngs){
this.Num=Rngs.Cells.Count,
this.arr=Rngs.Value().flat(),
this.Sum=function(){return this.arr.reduce((x,y)=>x+y)},//调用内部对象如:arr,必须在前方加this
this.Max=this.arr.reduce((x,y)=>(x>y)?x:y),//调用内部对象如:arr,必须在前方加this
this.Rngcolor=function(color){Rngs.Interior.ColorIndex=color}
}
function test(){
var total=new custom(Range("b2:d2"));//自定义类添加单元格区域,后面调用函数就不需要再添加单元格区域。如下:
Console.log(total.Max);
Console.log(total.Sum());
total.Rngcolor(9)//添加颜色号
var demo=new custom(Range("b4:d10"));//可同时对多个区域使用自定义类
Console.log(demo.Max);
Console.log(demo.Sum());
demo.Rngcolor(5)//添加颜色号
}
javascript
//7-13 WPS JS宏 this 用构造函数自定义类-2,调用内部对象必须用this
function custom(Rngs){
this.Num=Rngs.Cells.Count,
this.arr=Rngs.Value().flat(),
this.Sum=function(){return this.arr.reduce((x,y)=>x+y)},//调用内部对象如:arr,必须在前方加this
this.Max=this.arr.reduce((x,y)=>(x>y)?x:y),//调用内部对象如:arr,必须在前方加this
this.Rngcolor=function(color){Rngs.Interior.ColorIndex=color}
}
function test(){
var total=new custom(Range("b2:d2"));//自定义类添加单元格区域,后面调用函数就不需要再添加单元格区域。如下:
Console.log(total.Max);
Console.log(total.Sum());
total.Rngcolor(9)//添加颜色号
var demo=new custom(Range("b4:d10"));//可同时对多个区域使用自定义类
Console.log(demo.Max);
Console.log(demo.Sum());
demo.Rngcolor(5)//添加颜色号
}