gpt4 book ai didi

javascript - 在 Javascript 中通过原型(prototype)访问 Date 对象的方法

转载 作者:行者123 更新时间:2023-11-28 01:55:24 27 4
gpt4 key购买 nike

我试图通过使用 getTime 对象的原型(prototype)来继承/访问 Date 对象的所有属性/方法,但我遗漏了一些东西。请给我指出正确的方向。

//empty constructor 
function getTime(){}

//attempt to inherit all of the properties and methods of Date object
getTime.prototype = new Date();

//create a new Date object and test
//this works
var wt = new Date();
alert(wt);

//create a new getTime object and test.
//I was uner the impression that getTime object
//now has access to all the properties/methods of the Date object.
//'This generates:TypeError this is not a Date object.'
var wt2 = new getTime();
alert(wt2.getHours());

JSfiddle:http://jsfiddle.net/nysteve/QHumL/12/

最佳答案

对 Date 对象原型(prototype)的访问实际上是有效的,但这些方法期望处理 Date 类型的对象。更多详细信息请参见:https://stackoverflow.com/a/7141488/2847629

关于javascript - 在 Javascript 中通过原型(prototype)访问 Date 对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19191353/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com