Javascript中的构造函数属性是否有一个很好的用例? [英] Is there a good use case for the constructor property in Javascript?

查看:215
本文介绍了Javascript中的构造函数属性是否有一个很好的用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这个问题是不是构造函数属性做什么? - 有很多关于它究竟是什么以及它是如何工作的良好文档:它是对创建对象的函数的引用(可以从其原型继承)。

First off, this question is not "what does the constructor property do?" - There's plenty of good documentation on exactly what it is and how it works: It's a reference to the function that created the object (which may be inherited from its prototype).

我对了解这个属性的常见用例更感兴趣。它在理论上似乎都很好,但是什么时候你实际上需要对构造你的对象的函数的引用?一些想法是:

I'm more interested in knowing common use-cases for this property. It seems all good in theory, but when would you actually need a reference to the function that constructed your object? A few ideas would be:


  • 也许我想要克隆它。我可以再次调用构造函数,并且
    获取我的对象的另一个实例。这当然不能很好地运行
    ,因为你可能正在创建一个对象的实例,而不是对象本身的
    原型;加上一个更喜欢的方法,
    可以创建一个新对象并设置该对象的原型。

  • 也许你可以用它来弄清楚对象的类型
    是。这看起来很奇怪,因为您可以使用 instanceof
    Object.prototype.toString()

  • 您可以更改或重新分配构造函数吗?有没有充分的理由
    这样做?

  • Perhaps I want to clone it. I could call the constructor again and
    get another instance of my object. This of course wouldn't work well since you'd potentially be creating an instance of your object's
    prototype, not the object itself; plus a much preferred method would be to create a new object and set that object's prototype instead.
  • Perhaps you can use it to figure out what the "type" of the object is. This seems rather odd, since you can use instanceof or Object.prototype.toString() instead.
  • Can you change or re-assign the constructor? Would there ever be a good reason to do this?

希望有些人可以使用一些好的Javascript模式来填写使用构造函数引用,或提供属性存在的官方解释。

Hopefully some people can chime in with some good Javascript paterns that make use of the constructor reference, or provide an official explanation for why the property exists.

推荐答案

构造函数属性的一种情况方便(或者如果它是可靠的)是函数需要知道它已经传递的参数类型的地方,例如

One case where the constructor property is handy (or would be if it was reliable) is where a function needs to know the type of argument it has been passed, e.g.

function foo(arg) {
  if ( /* if arg is an array */ ) {
    // deal with array
  } else if ( /* if arg is an object */ ) {
    // deal with object
  }
}

如果上面的函数传递了一个数组或对象,那么 typeof 将在两种情况下都返回 object 。可以使用构造函数属性:

If the above function is passed an array or object, then typeof will return object in both cases. The constructor property can be used:

  if ( arg.constructor == Array )

但如果数组是在与测试发生的不同的帧中创建的(即它的Array构造函数是与Array不同的对象),则会失败函数在测试范围内)。

But that fails if the array is created in a different frame to where the test is taking place (i.e. it's Array constructor is a different object to the Array function in the scope of the test).

因此,如果排除框架(或其他范围是问题的情况),那么构造函数属性可以用于这个。

So if you rule out frames (or other cases where scope is an issue), then the constructor property is fine to use for this.

但这并不能解决构造函数属性可写的一般问题(因此可以设置为任何东西)以及原型链不仅仅是微不足道的情况。

But that does not fix the general issue of the constructor property being writable (and therefore can be set to anything) and cases where the prototype chain is more than trivial.

这篇关于Javascript中的构造函数属性是否有一个很好的用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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