我可以直接调用 Date 对象的 parse 方法,如下所示:
alert(Date.parse("March 21, 2012"));
但是我不能这样做:
alert(Date.getTime()); // TypeError: Date.getTime is not a function
这就是我让它工作的方式:
alert(new Date().getTime()); // works well
那么为什么我不能像 Date.parse() 一样直接调用 Date.getTime() 呢?
基本问题:我编写了一个类,我想直接使用它的一些方法,如上面的 Date.parse() 。
最佳答案
getTime
位于 Date.prototype
中,在构造 new Date()
对象时使用。
parse
位于 Date
本身中,因此是直接调用的,而不是从构造的对象中调用。
这是a post about JavaScript prototypes为了您的阅读乐趣。
关于javascript - 两种方法有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12031172/