有没有办法在将对象推入Typescript中的数组之前排除函数?

时间:2017-03-23 15:41:13

标签: javascript arrays typescript

当我将一个对象推入一个数组时,它会推动对象及其方法,就像makeaction()一样,但是我不想在数组中看到它们,有没有办法将它们排除在外

我在控制台上得到了这个

[ Pilot {
makeaction: [Function],
_name: 'Michael',
_lstname: 'Kiddies',
_age: 50,
_weight: 80,
_expyear: 8,
vec: 
 Car {
   _price: 3500000,
   _weight: 1300,
   _speed: 360,
   _power: 650,
   _brand: 'Aston Martin',
   _model: 'Vulcan',
   _torque: 1200,
   _person: [Circular] } },
Pilot {
makeaction: [Function],
_name: 'Enzo',
_lstname: 'LaFerrari',
_age: 23,
_weight: 90,
_expyear: 20,
vec: 
 Car {
   _price: 3500000,
   _weight: 1200,
   _speed: 320,
   _power: 750,
   _brand: 'Ferrari',
   _model: 'FXXK',
   _torque: 900,
   _person: [Circular] } },
Pilot {
makeaction: [Function],
_name: 'Austin',
_lstname: 'Texas',
_age: 30,
_weight: 56,
_expyear: 2,
vec: 
 Ship {
   _price: 5000000,
   _weight: 15000,
   _speed: 50,
   _power: 7000,
   _shipname: 'Evelin Mærsk',
   _beam: 20,
   _lenghtov: 55,
   _person: [Circular] } }]

此外,我想删除数组中的_person: [Circular]

这些是我的一些课程

Pilot.ts

 export class Pilot extends Person implements PersonInterface {
    private vec: Vehicle;
    private _expyear: number;

    constructor(name?, lstname?, age?, weight?, expyears?, vec?: Vehicle) {
        super(name, lstname, age, weight);
        this.vec = vec;
        this._expyear = expyears;
        vec.person = this;
    }

    get expyear(): number {
        return this._expyear;
    }

    set expyear(value: number) {
        this._expyear = value;
    }

    public makeaction = () => {
        return this.vec.makeaction();
    }
}

Car.ts

export class Car extends Vehicle {
    private _brand: string;
    private _model: string;
    private _torque: number;

    constructor(brand?, model?, torque?, weight?, speed?, pow?, price?) {
        super(weight, speed, pow, price);
        this._brand = brand;
        this._model = model;
        this._torque = torque;
    }

    get brand(): string {
        return this._brand.trim();
    }

    set brand(value: string) {
        this._brand = value;
    }

    get model(): string {
        return this._model;
    }

    set model(value: string) {
        this._model = value;
    }

    get torque(): number {
        return this._torque;
    }

    set torque(value: number) {
        this._torque = value;
    }

    public makeaction() {
        if (this.person.constructor.name == `Customer`) {
            return `The ${this.person.constructor.name} ${this.person.name} ${this.person.lstname} has bought the ${this.constructor.name} ${this.brand + ` ` + this.model} for only ${prf.convertMillions(this.price)}`;
        }
        else {
            return `The ${this.person.constructor.name} ${this.person.name} ${this.person.lstname} is driving the ${this.constructor.name} ${this.brand + ` ` + this.model}`;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

public makeaction = () => {的问题及其在控制台中显示的原因是它是一个类属性而不是原型方法。它是在构造函数中创建并赋予箭头函数的属性。你想让它成为一种常规方法:

public makeaction() {
    …

这样,它隐含地不可枚举(至少在ES6 class中),并且不会被控制台漂亮的打印机考虑,因为它是继承的。