Javascript 将属性复制到继承类型的目的

Javascript 将属性复制到继承类型的目的,javascript,inheritance,prototype,typescript,Javascript,Inheritance,Prototype,Typescript,在使用联机TypeScript编译器时,我注意到创建者选择通过以下方法实现继承(我已使其更具可读性): 我很难理解\uu extends方法的实现。 从扩展类型手动复制所有属性的目的是什么?继承原型不会解决这个问题吗 与直接复制的属性相比,在原型树中搜索属性时是否存在性能损失 第二部分我也不明白这个目的。而不是这样做: function __() { this.constructor = type; } __.prototype = base.prototype. type.protot

在使用联机TypeScript编译器时,我注意到创建者选择通过以下方法实现继承(我已使其更具可读性):

我很难理解
\uu extends
方法的实现。
从扩展类型手动复制所有属性的目的是什么?继承原型不会解决这个问题吗

与直接复制的属性相比,在原型树中搜索属性时是否存在性能损失

第二部分我也不明白这个目的。而不是这样做:

function __() {
    this.constructor = type;
}
__.prototype = base.prototype.
type.prototype = new __();
这不能简单地实现为:

type.prototype = base.prototype;

扩展复制原型和静态成员(浅复制)。Statics是Parent.things和prototype是Parent.prototype.things复制prototype.things的方式是对象创建的多边形填充的工作方式

var __extends = this.__extends || function (d, b) {
    //Copies Item.things to Sword.things
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    //Copies Item.prototype.things to Sword.prototype.things
    //this is a polyfill for Object.create
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var Sword = (function (_super) {
    __extends(Sword, _super);
    function Sword() {
        _super.apply(this, arguments);
    }
    return Sword;
})(Iten);
考虑以下类型脚本:

class Item {
    public static DAMAGE = 0;
};
在JavaScript中,这将是:

var Item = (function () {
    function Item() {
    }
    Item.DAMAGE = 0;
    return Item;
})();
我如何使用静电损伤?请参见以下示例(简化的JS)


关于构造函数、原型、继承、混入和此值的介绍:

好问题。让我们来分析一下

var __extends = function(type, base) {
    // ** Begin static-side inheritance **
    for (var prop in base) {
        if (base.hasOwnProperty(prop)) {
            type[prop] = base[prop];
        }
    }
    // ** End static-side inheritance **

    // ** Begin Object.create polyfill **
    function __() { // <- ?
        this.constructor = type;
    }
    __.prototype = base.prototype.
    // ** End Object.create polyfill **

    // ** Prototype setup **
    type.prototype = new __();
};
请注意,在最后一行中,
Truck
类通过其基类
Car
获得了其
create
方法。需要复制,因为
Truck.\uuu proto\uu
不是
Car

接下来是
对象。创建
polyfill。编写
type.prototype=base.prototype
是不够的,因为
\uuuu proto\uuuuuu
(又称
[[proto]]]
)和
prototype
成员不是一回事。关于这一点的其他答案给出了一个更好的原型遗传概述,所以我将遵从这些


最后一行设置构造函数的原型,以便创建的对象具有正确的
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。创建了一个如何使用静态的示例,Rayan的示例展示了一个很好的工厂模式示例。回顾这个问题的答案,现在我对TS有了更多的经验,我觉得您的答案更完整、信息更丰富,并且提供了更多的见解。改为这是正确的答案。
var Item = (function () {
    function Item() {
    }
    Item.DAMAGE = 0;
    return Item;
})();
//Using static to make general getters and setters
var Item = function(damage){ 
  this._data = [];
  this.set(Item.DAMAGE,damage)
};
Item.DAMAGE=0;
Item.prototype.get=function(dataIndex){
  return this._data[dataIndex];
};
Item.prototype.set=function(dataIndex,val){
  //maybe execute Item.rules[dataIndex].set(val) for validating value
  this._data[dataIndex] = val;
  return val;
};
var i = new Item(22);
//I could get damage using i.get(0)
//but the following reads more easily
i.get(Itesm.DAMAGE);
var __extends = function(type, base) {
    // ** Begin static-side inheritance **
    for (var prop in base) {
        if (base.hasOwnProperty(prop)) {
            type[prop] = base[prop];
        }
    }
    // ** End static-side inheritance **

    // ** Begin Object.create polyfill **
    function __() { // <- ?
        this.constructor = type;
    }
    __.prototype = base.prototype.
    // ** End Object.create polyfill **

    // ** Prototype setup **
    type.prototype = new __();
};
class Car {
    static create(name: string, topSpeed: number): Car {
        // ...
        return new this(/* ... */);
    }
}

class RaceCar extends Car {
    // RaceCar ignores topSpeed because it goes super fast
    static create(name: string): RaceCar {
        // ...
        return new this(/* ... */);
    }   
}

class Truck extends Car {
}

function makeTestCar(t: typeof Car) {
    return t.create('test', 60);
}
var car = makeTestCar(Car); // returns an instance of Car
var raceCar = makeTestCar(RaceCar); // returns an instance of RaceCar
var truck = makeTestCar(Truck); // OK, even though Truck doesn't explicitly implement 'create'