我真的看不到为什么该
constructor属性就是JS中的属性。我偶尔会发现自己使用它来获取IE
<9中对象的原型(例如Event对象)。但是,我确实认为它可以允许某些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();//so far the set-upfunction 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}
因此AFAIK,构造函数属性的 “优点” 是:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)