Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.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
javascript对象与阅读文档-初学者对效率的质疑_Javascript_Object - Fatal编程技术网

javascript对象与阅读文档-初学者对效率的质疑

javascript对象与阅读文档-初学者对效率的质疑,javascript,object,Javascript,Object,我正在创建一个包含天文计算的网页,其中用户可以更改日期/时间/纬度/经度等,并相应地重新计算网页 我已经创建了一个对象-一个全局对象-具有许多属性和更新这些属性的方法,但现在我对此提出质疑 当我需要重新计算时,读取页面元素是否更好/更高效,或者是否将对象一直保存在内存中 以下是对象定义: function TIME_OBJ () { var d = new Date(); //Properties this.LHour = d.getHours(); this.LMinute = d.getM

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

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

当我需要重新计算时,读取页面元素是否更好/更高效,或者是否将对象一直保存在内存中

以下是对象定义:

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;
}

因此,如果日期改变,这将影响当地恒星时间、格林威治时间、世界时、日出/日落、月出/日落等等。这意味着,如果我正在读取页面元素,我将不得不进行许多getElementById调用来获取数据,而不是设置了属性的全局对象,我只需要在计算完成后页面元素

感谢你的想法。。。。 达斯


另外,DOM是页面的正确术语吗?

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

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

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

希望这有帮助