在Javascript中使用new和不使用它一样吗?

发布于 2024-12-22 14:26:40 字数 413 浏览 1 评论 0原文

考虑这段代码:

function klass( z ) {
    this.a = z;
    return this;
}

var b = klass( 5 );
var c = new klass( 9 );

当我在 Chrome 中运行它并在控制台中检查时,b 结果是 DOMWindow 类型,而 c 是输入klass

尽管两者都具有属性 a,但实际上两者都是 klass实例

  • 使用new和不使用new一样吗?
  • 在这个例子中是一样的,但在其他情况下是不同的吗?
  • 效率或行为是否存在差异?

Consider this code:

function klass( z ) {
    this.a = z;
    return this;
}

var b = klass( 5 );
var c = new klass( 9 );

When I run it in Chrome and check in the console, b turns out to be of type DOMWindow, while c is of type klass.

Although both have the property a, effectively both being an instance of klass.

  • Is using or not using new, the same?
  • Is it the same on this example but different in other situations?
  • Are there differences in efficiency or behavior?

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

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

发布评论

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

评论(3

一花一树开 2024-12-29 14:26:40

当像这样调用函数时,

klass(6);  //called function invocation

this 将被设置为全局对象,或者,如果您处于严格模式,则 undefined

结果,第一个示例(没有new) 将返回附加了新 a 属性的全局对象。在严格模式下,它会抛出错误,因为 this 将被设置为 undefined,并且您无法将 a 属性添加到 未定义。

当您使用 new 调用函数时,

new klass( 9 );  //called constructor invocation

this 值将设置为一个新对象,并从该函数隐式返回 — 无需说 return this

为了完整起见,当您将函数作为对象的方法调用时:

foo.method();  //called method invocation

this 将被设置为 this 中的对象 - foo案件。

当您使用 apply(或 call)调用函数时,

method.apply(foo)  //called apply invocation 

this 会设置为您指定的任何内容 - foo 再次

编辑

我提到了 strict我的回答中的模式。如果页面

"use strict"

位于最顶部,则使用严格模式。

When a function is invoked like this

klass(6);  //called function invocation

this will be set to the global object, or, if you're in strict mode, undefined

As a result, the first example (without the new) will return the global object with a new a property attached. In strict mode it will throw an error since this will be set to undefined, and you can't add an a property to undefined.

When you invoke a function with new

new klass( 9 );  //called constructor invocation

the this value is set to a new object, and is implicitly returned from the function—there's no need to say return this

For completeness, when you invoke a function as a method on an object:

foo.method();  //called method invocation

this will be set to the object—foo in this case.

And when you invoke a function with apply (or call)

method.apply(foo)  //called apply invocation 

this is set to whatever you specify—foo again

EDIT

I mentioned strict mode in my answer. A page uses strict mode if it has

"use strict"

at the very top of it.

述情 2024-12-29 14:26:40

如果没有 new,您的函数将仅对 this 绑定到的任何内容进行操作,在您的情况下是 DOMWindow。没有创建新实例,而是在 window 对象上设置 a 属性。调用该方法两次会破坏之前的结果。

试试这个:

var b = klass(5)
log.info(b.a)
log.info(b == window)  // b is actually the window object
log.info(window.a)   // also 5

var c = klass(6)
log.info(b.a)     // now set to 6
log.info(c == b)  // c and b are the same

Without the new your function will just operate on whatever this was bound to, in your case the DOMWindow. No new instance is created, the a property gets set on the window object. Calling the method twice clobbers the previous result.

Try this:

var b = klass(5)
log.info(b.a)
log.info(b == window)  // b is actually the window object
log.info(window.a)   // also 5

var c = klass(6)
log.info(b.a)     // now set to 6
log.info(c == b)  // c and b are the same
我早已燃尽 2024-12-29 14:26:40

这绝对不一样:

var a = klass(42);
console.log(a.a); // 42
var b = klass(69);
console.log(b.a); // 69
console.log(a.a); // 69

如果你不调用new,你就不会得到任何新的东西。

It's absolutely not the same:

var a = klass(42);
console.log(a.a); // 42
var b = klass(69);
console.log(b.a); // 69
console.log(a.a); // 69

If you don't call new, you're not getting anything new.

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