我正在为JXA中的某些视频编辑软件构建API - 适用于Mac OS X的自动化JavaScript .API实现的最重要的“类型”之一是TimeFrame类型。简而言之,它看起来像这样:hh:mm:ss:ff
这是我的函数,它产生类型:
function TimeFrame(val){
if(typeof val == "string"){
regex = /(\d\d):(\d\d):(\d\d):(\d\d)/
arr = regex.exec(val)
h = parseInt(arr[1])
m = parseInt(arr[2])
s = parseInt(arr[3])
f = parseInt(arr[4])
this.frames = f + s*30 + m*60*30 + h*60*60*30
} else {
this.frames = Math.round(val)|0
}
this.toString = function(){
t = this.frames
h = parseInt(t/30/60/60)
m = parseInt(t/30/60%60)
s = parseInt(t/30%60)
f = t%30
//Pad puts "0" infront of single digit values.
function pad(obj) {
return obj.toString().length > 1 ? obj : "0" + obj
}
return pad(h) + ":" + pad(m) + ":" + pad(s) + ":" + pad(f)
}
this.valueOf = function(){
return this.frames
}
}
该功能将按如下方式使用
var ti = new TimeFrame("00:01:32:10") //1 minute 32 seconds and 10 frames
var tf = new TimeFrame("00:01:32:12") //1 minute 32 seconds and 12 frames
var td = new TimeFrame(tf-ti)
td.toString() // returns 00:00:00:02, or 2 frames.
这只是一个如何找到两个示例区间之间差异的示例。但是,如果没有以默认方式初始化函数,则会出现问题:
var t = new TimeFrame(30)
t.toString() //returns "00:00:01:00" as expected
var t = new TimeFrame()
t.toString() //returns "00:00:00:00" as expected
t.frames = 30
t.toString() //returns "0" ?!? It should return "00:00:01:00" as above...
有谁知道为什么会这样?