什么是`constructor`属性真正用于? [英] What is the `constructor` property really used for?

查看:211
本文介绍了什么是`constructor`属性真正用于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,每个函数的原型对象都有一个非枚举属性构造函数,它指向函数( EcmaScript§13.2)。它不用于任何本机功能(例如 instanceof 只检查原型链),但我们在覆盖函数的原型属性以继承时鼓励调整

  SubClass.prototype = Object.create(SuperClass.prototype,{
constructor:{value:SubClass,writable:true,configurable:true}
}但是,我们(包括我)是否只是为了清晰和整洁才做这个呢?/ b $ b



是否有任何依赖于 构造函数属性的真实世界用例

解决方案

我真的不明白为什么构造函数是什么在JS。我偶尔发现自己使用它来获得对象(如Event对象)的原型在IE&然而我认为它允许一些ppl模仿古典的OO编程结构:

  function Foo()
{
this.name ='Foo';
}
function Bar()
{
this.name ='Bar';
}
function Foobar(){};
Foo.prototype = new Foobar;
Foo.prototype.constructor = Foo;
Bar.prototype = new Foobar;
Bar.prototype.constructor = Bar;
var foo = new Foo();
var bar = new Bar();
//到目前为止的设置
function pseudoOverload(obj)
{
if(!(obj instanceof Foobar))
{
throw new错误'我只接受Foobar的子类';
}
if(obj.constructor.name ==='Foo')
{
return new obj.constructor; //对构造函数的引用非常方便
}
//使用Bar实例做事情
}

$ b
  • 能够将对象分组为某个类的子类,但仍然可以检查您正在处理的特定类型的子类。

  • 正如你所说:整洁。


  • In JavaScript, every function's prototype object has a non-enumerable property constructor which points to the function (EcmaScript §13.2). It is not used in any native functionality (e.g. instanceof checks only the prototype chain), however we are encouraged to adjust it when overwriting the prototype property of a function for inheritance:

    SubClass.prototype = Object.create(SuperClass.prototype, {
        constructor: {value:SubClass, writable:true, configurable:true}
    });
    

    But, do we (including me) do that only for clarity and neatness? Are there any real-world use cases that rely on the constructor property?

    解决方案

    I can't really see why the constructor property is what it is in JS. I occasionally find myself using it to get to the prototypes of objects (like the Event object) in IE < 9. However I do think it's there to allow some ppl to mimic classical OO programming constructs:

    function Foo()
    {
        this.name = 'Foo';
    }
    function Bar()
    {
        this.name = 'Bar';
    }
    function Foobar(){};
    Foo.prototype = new Foobar;
    Foo.prototype.constructor = Foo;
    Bar.prototype = new Foobar;
    Bar.prototype.constructor = Bar;
    var foo = new Foo();
    var bar = new Bar();
    //so far the set-up
    function pseudoOverload(obj)
    {
        if (!(obj instanceof Foobar))
        {
            throw new Error 'I only take subclasses of Foobar';
        }
        if (obj.constructor.name === 'Foo')
        {
            return new obj.constructor;//reference to constructor is quite handy
        }
        //do stuff with Bar instance
    }
    

    So AFAIK, the "advantages" of the constructor property are:

    • instantiating new objects from instance easily
    • being able to group your objects as being subclasses of a certain class, but still being able to check what particular type of subclass you're dealing with.
    • As you say: being tidy.

    这篇关于什么是`constructor`属性真正用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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