Javascript 为什么';我的对象不返回日期吗?

Javascript 为什么';我的对象不返回日期吗?,javascript,Javascript,我正在尝试用javascript测试对象。为什么我的对象不返回日期 <script type="text/javascript"> function test() { var date = new Date(); return date.getMilliseconds(); } var s = new test(); console.log(s); </script> 功能测

我正在尝试用javascript测试对象。为什么我的对象不返回日期

<script type="text/javascript">
    function test() {
        var date = new Date(); 
        return date.getMilliseconds();            
    }

    var s = new test();
    console.log(s);

</script>

功能测试(){
变量日期=新日期();
返回日期。getmillizes();
}
var s=新测试();
控制台日志;
应该是

var s = test();
new
关键字用于创建新对象,在这种情况下,构造函数只能返回非基本对象。因为您只想返回毫秒,所以在不使用新的
的情况下调用
test()
尝试以下操作:

如果要创建test()的实例


它返回毫秒吗?它说它只返回test。Console.log typeof表示它是一个对象。当我有了新的关键字,如果我删除了它,会发生什么?它在没有new test()的情况下工作,因此s成为一个新对象。我的返回日期。GetMillimess()会发生什么变化?它被丢弃了吗?是的,s成为test的一个新实例,返回值被忽略,因为它是一个基元类型。如果test()返回这个“return{milistesons:date.getMillistics()};”,那么s=new test()将是一个对象。
var s = test();
var test = (function () {
    function test() {
        this.date = new Date();
    }
    test.prototype.getMilliseconds = function () {
        return this.date.getMilliseconds();
    };
    return test;
})();

var s = new test().getMilliseconds();
console.log(s);