javascript对象与阅读文档——初学者查询效率

javascript object vs reading the document - beginners query on efficiency

本文关键字:初学者 查询 效率 文档 对象 javascript      更新时间:2023-09-26

我正在创建一个包含一些天文计算的网页,用户可以在该网页上更改日期/时间/纬度/经度等,并相应地重新计算页面。

我已经创建了一个对象,一个全局对象,其中包含许多属性和更新这些属性的方法,但现在我对此提出了质疑。。。。

当我需要重新计算时,读取页面元素是更好/更高效,还是让对象一直在内存中。

以下是对象定义:

function TIME_OBJ () {
var d = new Date();
//Properties
this.LHour = d.getHours();
this.LMinute = d.getMinutes();
this.LSecond = d.getSeconds();
this.TZone = (d.getTimezoneOffset() / 60) * -1;
this.LDay = d.getDate();
this.LMonth = d.getMonth() + 1;
this.LYear = d.getFullYear();
this.UHours = 0;
this.UMinutes = 0;
this.USeconds = 0;
this.UDay = 0;
this.UMonth = 0;
this.UYear = 0;
this.UTString = "";
this.GSTHour = 0;
this.GSTMinute = 0;
this.GSTSecond = 0;
this.GSTString = "";
this.LSTHour = 0;
this.LSTMinute = 0;
this.LSTSecond = 0;
this.LSTString = "";
this.GDay = 0;
this.GMonth = 0;
this.GYear = 0;
this.GHour = 0;
this.GMinute = 0;
this.GSecond = 0;
this.GString = "";
this.LJD = 0;
this.UJD = 0;
this.LMJD = 0;
this.UMJD = 0;
this.LDoW = 0;
this.UDoW = 0;
this.LDayN = 0;
this.UDayN = 0;
this.LatD = 52;
this.LatM = 0;
this.LatS = 0;
this.LatNS = "S";
this.LatDD = 0;
this.LonD = 0;
this.LonM = 30;
this.LonS = 0;
this.LonEW = "W";
this.LonDD = 0;

//Methods
this.UpdateAll = UpdateAll;
this.UpdateDate = UpdateDate;
this.SetClean = SetTimeClean;
this.GetUT = UpdateUT;
this.GetLJD = UpdateJD;
this.GetUJD = UpdateJD;
this.GetLMJD = UpdateMJD;
this.GetUMJD = UpdateMJD;
this.GetLDoW = UpdateDoW;
this.GetUDoW = UpdateDoW;
this.GetLDayN = UpdateDayN;
this.GetUDayN = UpdateDayN;
this.GetGrDate = UpdateGD;
this.GetGST = UpdateGST;
this.GetLST = UpdateLST;
this.GetDecPos = UpdateDecPos;
this.Dirty = false;

}

因此,如果日期发生变化,这将影响本地恒星时间、格林尼治时间、世界时、日出/set、月升/set等。这意味着,如果我在读取页面元素,我将不得不进行多次getElementById调用来获取数据,而不是让一个已经设置了属性的全局对象,我只需要在计算完成后对元素进行分页。

感谢你的想法。。。。Daz

第页。S.DOM是页面的正确术语吗??

在内存中保留引用总是比搜索DOM(页面上的元素)快得多。但是,您可以在加载页面的过程中保留对这些元素的引用,并只使用它们。遍历DOM以找到合适的元素是非常昂贵的。

读取对象比查询DOM(是的,DOM是正确的:-)要快得多,即使使用getElementByID也是最好的查询方式。

同样,从设计角度来看,这更好,因为您可以将对象作为自包含的bundle传递给另一个函数,而不是让函数重新计算DOM中的值。这也允许您编写面向对象的代码(更整洁、更干净的代码)。

希望这能有所帮助。