具体来说,哪个是正确的?
new Date().getTime();
或者
(new Date()).getTime();
谢谢
如前所述,两者都是正确的。我想问题更多是关于new
操作员的工作。它的存在可以通过以下示例描述:
var constr = function() {
return {
getConstructor: function() {
return Date
}
};
};
console.log(new constr()); // Object { getConstructor: function }
console.log(new constr().getConstructor); // function () { return Date }
console.log(new constr().getConstructor()); // function Date() { [native code] }
console.log(new (constr().getConstructor())); // Wed Mar 25 2015 20:10:07 GMT+0500 (YEKT)
因此,如果要创建由嵌套函数返回的构造函数的实例,则应考虑使用new (code that returns constructor)
而不是new [code that eventually returns constructor]
。后者将不起作用,因为new
它将选择第一个函数调用并创建一个实例。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句