JavaScript的:将参数传递给对象的构造函数 [英] javascript: pass arguments to object constructor

查看:116
本文介绍了JavaScript的:将参数传递给对象的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个jQuery lib和我需要实现日期时间函数。
我需要创建一个返回一个新的Date对象.Date()函数。

i am writing a jquery lib and i need to implement Datetime functions. i need to create a .Date() function which return a new Date object.

如何功能我.Date(参数)参数传递给Date对象构造函数,以便创建一个新的约会对象?

How to pass arguments of my .Date(args) function to Date object constructor so to create a new date object?

我想这样的事情,我是插件的命名空间,我= $。jqGUI。

i tried something like this, me is the plugin namespace, me=$.jqGUI.

        //.Date(Value)
        /* Return a date object.
         * ReturnValue = Date(Value)
         */
        me.Date = function(Value){
            //pass arguments to Date constructor
            var sDate = Date.prototype.constructor.apply(null, arguments);

            console.log(sDate);

            //create a date object d
            var d = new Date(sDate);

            //validate date object
            if(d.toDateString()=="Invalid Date"){
                throw new Error("$.jqGUI.Date: Invalid Date");
            }
            else{
                return d;                   
            }
        };

在这里,我传递给Date.prototype.constructor的论点,但我在任何情况下,当前的日期得到。
如果参数是一个日期字符串被忽略。
为什么?

here i pass to Date.prototype.constructor the arguments but i get in any case the current date. if arguments is a date string it is ignored. why?

推荐答案

确定为我下面的工作了。

ok for me the following was worked

var args = [];
for(var i = 0; i < arguments.length; i++)
   args.push("arguments[" + i + "]");

var d = eval("new Date(" + args.join(",") + ")");   

这篇关于JavaScript的:将参数传递给对象的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆