为什么 JavaScript 中的“Date”实例需要“new”关键字?

发布于 2024-09-16 00:32:58 字数 609 浏览 11 评论 0原文

我了解行为的差异。 Date() 返回一个表示当前日期的字符串,new Date() 返回一个 Date 对象的实例,我可以调用该对象的方法。

但我不知道为什么。 JavaScript 是原型化的,因​​此 Date 是一个函数一个对象,该对象具有也是对象的成员函数(方法)。但我还没有编写或读过任何具有这种行为方式的 JavaScript,我想了解其中的区别。

有人可以向我展示一些函数的示例代码,该函数具有一个方法,使用 new 运算符返回一个实例,并在直接调用时输出一个字符串吗?即这样的事情是怎么发生的?

Date();                   // returns "Fri Aug 27 2010 12:45:39 GMT-0700 (PDT)"
new Date();               // returns Object
new Date().getFullYear(); // returns 2010
Date().getFullYear();     // throws exception!

非常具体的要求,我知道。我希望这是一件好事。 :)

I understand the difference in behavior. Date() returns a String representing the current date, and new Date() returns an instance of the Date object whose methods I can call.

But I don't know why. JavaScript is prototyped, so Date is a function and an object which has member functions (methods) which are also objects. But I haven't written or read any JavaScript that behaves this way, and I'd like to understand the difference.

Can somebody show me some sample code of a function that has a method, returns an instance with the new operator, and outputs a String when called directly? i.e. how does something like this happen?

Date();                   // returns "Fri Aug 27 2010 12:45:39 GMT-0700 (PDT)"
new Date();               // returns Object
new Date().getFullYear(); // returns 2010
Date().getFullYear();     // throws exception!

Very specific request, I know. I hope that's a good thing. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆流年 2024-09-23 00:32:58

其中大部分可以自己完成。根据 ECMA 规范,调用不带 new 的裸构造函数并获取字符串对于 Date 是特殊的,但您可以为此模拟类似的操作。

这就是你应该如何做的。首先在构造函数模式中声明一个对象(例如,要使用 new 调用的函数,并返回其 this 引用:

var Thing = function() {
    // Check whether the scope is global (browser). If not, we're probably (?) being
    // called with "new". This is fragile, as we could be forcibly invoked with a 
    // scope that's neither via call/apply. "Date" object is special per ECMA script,
    // and this "dual" behavior is nonstandard now in JS. 
    if (this === window) { 
        return "Thing string";
    }

    // Add an instance method.
    this.instanceMethod = function() {
        alert("instance method called");
    }

    return this;
};

Thing 的新实例可以具有 instanceMethod() 调用它们。现在只需在 Thing 本身上添加一个“静态”函数:

Thing.staticMethod = function() {
    alert("static method called");
};

现在您可以执行以下操作:

var t = new Thing(); 
t.instanceMethod();
// or...
new Thing().instanceMethod();
// and also this other one..
Thing.staticMethod();
// or just call it plain for the string:   
Thing();

Most of this is possible to do yourself. Calling the bare constructor without new and getting a string is special for Date per the ECMA spec, but you can simulate something similar for that.

Here's how you'd do it. First declare an object in the constructor pattern (e.g. a function that is intended to be called with new and which returns its this reference:

var Thing = function() {
    // Check whether the scope is global (browser). If not, we're probably (?) being
    // called with "new". This is fragile, as we could be forcibly invoked with a 
    // scope that's neither via call/apply. "Date" object is special per ECMA script,
    // and this "dual" behavior is nonstandard now in JS. 
    if (this === window) { 
        return "Thing string";
    }

    // Add an instance method.
    this.instanceMethod = function() {
        alert("instance method called");
    }

    return this;
};

New instances of Thing can have instanceMethod() called on them. Now just add a "static" function onto Thing itself:

Thing.staticMethod = function() {
    alert("static method called");
};

Now you can do this:

var t = new Thing(); 
t.instanceMethod();
// or...
new Thing().instanceMethod();
// and also this other one..
Thing.staticMethod();
// or just call it plain for the string:   
Thing();
初吻给了烟 2024-09-23 00:32:58

new 是 Javascript(和其他语言)中的关键字,用于创建对象的新实例。
可能与 JavaScript 中的“new”关键字是什么? 重复。
另请参阅这篇文章:http://trephine.org/t/index.php?title =Understanding_the_JavaScript_new_keyword

new is a keyword in Javascript (and others) to create a new instance of an object.
Possibly duplicate of What is the 'new' keyword in JavaScript?.
See also this article: http://trephine.org/t/index.php?title=Understanding_the_JavaScript_new_keyword

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文