跳到主要内容

Vinyl.isCustomProp()

确定一个属性是否由 Vinyl 内部管理。Vinyl 在构造函数中设置值或在 clone() 实例方法中复制属性时使用此方法。

此方法在扩展 Vinyl 类时很有用。详见下方的扩展 Vinyl

用法

const Vinyl = require('vinyl');

Vinyl.isCustomProp('sourceMap') === true;
Vinyl.isCustomProp('path') === false;

签名

Vinyl.isCustomProp(property)

参数

参数类型说明
propertystring要检查的属性名称。

返回值

如果该属性不是内部管理的,则返回 true。

扩展 Vinyl

当自定义属性被内部管理时,必须扩展静态的 isCustomProp 方法,并在查询其中一个自定义属性时返回 false。

const Vinyl = require('vinyl');

const builtInProps = ['foo', '_foo'];

class SuperFile extends Vinyl {
constructor(options) {
super(options);
this._foo = 'example internal read-only value';
}

get foo() {
return this._foo;
}

static isCustomProp(name) {
return super.isCustomProp(name) && builtInProps.indexOf(name) === -1;
}
}

在上面的例子中,在克隆或将 options 传递给 new SuperFile(options) 时,foo_foo 不会被分配给新对象。

如果你的自定义属性或逻辑在克隆期间需要特殊处理,请在扩展 Vinyl 时重写 clone 方法。