Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将用于滚动平均的angular1 javascript循环队列实现转换为typescript_Javascript_Angular_Typescript_Angular2 Services - Fatal编程技术网

将用于滚动平均的angular1 javascript循环队列实现转换为typescript

将用于滚动平均的angular1 javascript循环队列实现转换为typescript,javascript,angular,typescript,angular2-services,Javascript,Angular,Typescript,Angular2 Services,我正在将项目从angular1迁移到angular2。 我有一张图表显示了移动平均线。为此,我需要使用循环队列,其中原型包含add、remove和getAverage方法 我用angular 1 javascript开发了以下代码,需要将其转换为typescript 有没有办法从typescript导入和调用javascript函数 或 有没有办法使用任何工具将javascript转换为typescript 请帮忙 'use strict'; angular.module('testeApp.t

我正在将项目从angular1迁移到angular2。 我有一张图表显示了移动平均线。为此,我需要使用循环队列,其中原型包含add、remove和getAverage方法

我用angular 1 javascript开发了以下代码,需要将其转换为typescript

有没有办法从typescript导入和调用javascript函数 或

有没有办法使用任何工具将javascript转换为typescript

请帮忙

'use strict';
angular.module('testeApp.testChart')
    .service('testRollingAverageSrc', function(){
            this.performRollingAverage = RollingAverage;
            function RollingAverage() {
                if (!(this instanceof RollingAverage)) {
                    // multiple conditions need to be checked to properly emulate Array
                    if (arguments.length > 1 || typeof arguments[0] !== 'number') {
                        return RollingAverage.apply(new RollingAverage(arguments.length), arguments);
                    } else {
                        return new RollingAverage(arguments[0]);
                    }
                }
                // if no arguments, then nothing needs to be set
                if (arguments.length === 0)
                    throw new Error('Missing Argument: You must pass a valid buffer length');

                // this is the same in either scenario
                this.size = this.start = this.end = 0;
                this.overflow = null;
                this.totalSum =0.0;

                // emulate Array based on passed arguments
                if (arguments.length > 1 || typeof arguments[0] !== 'number') {
                    this.data = new Array(arguments.length);
                    this.end = (this.length = arguments.length) - 1;
                    this.push.apply(this, arguments);
                } else {
                    this.data = new Array(arguments[0]);
                    this.end = (this.length = arguments[0]) - 1;
                }
                return this;
            }
            RollingAverage.prototype = {
                remove : function () {
                    var item;
                    if (this.size === 0) return;
                    item = this.data[this.end];
                    // remove the reference to the object so it can be garbage collected
                    if( !isNaN(this.data[this.end]) )
                        this.totalSum -= this.data[this.end];
                    delete this.data[this.end];
                    this.end = ((this.end - 1 + this.length) % this.length);
                    this.size--;
                    return item;
                },
                add : function () {
                    var i = 0;
                    // check if overflow is set, and if data is about to be overwritten
                    if (this.overflow && this.size + arguments.length > this.length) {
                        // call overflow function and send data that's about to be overwritten
                        for (; i < this.size + arguments.length - this.length; i++) {
                            this.overflow(this.data[(this.end + i + 1) % this.length], this);
                        }
                    }
                    // push items to the end, wrapping and erasing existing items
                    // using arguments variable directly to reduce gc footprint
                    for (i = 0; i < arguments.length; i++) {
                        // reduce the totalSum by the removed value
                        var value = this.data[(this.end + i + 1) % this.length];
                        if( !isNaN(value) )
                            this.totalSum -= value;
                        // save the new value
                        this.data[(this.end + i + 1) % this.length] = arguments[i];
                        // update totalSum
                        if( !isNaN((arguments[i])) )
                            this.totalSum += arguments[i];
                    }
                    // recalculate size
                    if (this.size < this.length) {
                        if (this.size + i > this.length) this.size = this.length;
                        else this.size += i;
                    }
                    // recalculate end
                    this.end = ((this.end + i) % this.length);
                    // recalculate start
                    this.start = (this.length + this.end - this.size + 1) % this.length;
                    return this.size;
                },
                avg : function () {
                    return this.size == 0 ? 0 : this.totalSum / this.size;
                }
            };
        });
“严格使用”;
angular.module('testeApp.testChart'))
.service('testRollingAverageSrc',function(){
this.performRollingAverage=滚动平均值;
函数RollingAverage(){
如果(!(此滚动平均值的实例)){
//需要检查多个条件才能正确模拟阵列
if(arguments.length>1 | |参数类型[0]!=='number'){
返回RollingAverage.apply(新的RollingAverage(arguments.length),arguments);
}否则{
返回新的RollingAverage(参数[0]);
}
}
//如果没有参数,则不需要设置任何内容
if(arguments.length==0)
抛出新错误('缺少参数:必须传递有效的缓冲区长度');
//这两种情况都是一样的
this.size=this.start=this.end=0;
this.overflow=null;
这是0.totalSum=0.0;
//基于传递的参数模拟数组
if(arguments.length>1 | |参数类型[0]!=='number'){
this.data=新数组(arguments.length);
this.end=(this.length=arguments.length)-1;
this.push.apply(this,arguments);
}否则{
this.data=新数组(参数[0]);
this.end=(this.length=参数[0])-1;
}
归还这个;
}
RollingAverage.prototype={
删除:函数(){
var项目;
如果(this.size==0)返回;
item=本次数据[本次结束];
//删除对对象的引用,以便可以对其进行垃圾收集
如果(!isNaN(this.data[this.end]))
this.totalSum-=this.data[this.end];
删除此.data[this.end];
this.end=((this.end-1+this.length)%this.length);
这个尺寸--;
退货项目;
},
添加:函数(){
var i=0;
//检查是否设置了溢出,以及数据是否即将被覆盖
if(this.overflow&&this.size+arguments.length>this.length){
//调用溢出函数并发送即将被覆盖的数据
对于(;ithis.length)this.size=this.length;
否则,这个.size+=i;
}
//重新计算结束
this.end=((this.end+i)%this.length);
//重新计算开始
this.start=(this.length+this.end-this.size+1)%this.length;
返回此.size;
},
平均值:函数(){
返回this.size==0?0:this.totalSum/this.size;
}
};
});

TypeScript的特点是它是JavaScript。只需将代码复制并粘贴到
.ts
文件中,它就可以工作了。将其重构为一种更为类型化的脚本方式可能是相关的,例如将其更改为私有方法,但没有任何东西可以阻止您的代码立即运行。

我尝试将代码原样放在.ts文件中。另外,尝试更改为@Injectable()导出类RollingAveragesService,但不起作用