Vinyl.isCustomProp()#

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

¥Determines if a property is internally managed by Vinyl. Used by Vinyl when setting values inside the constructor or when copying properties in the clone() instance method.

该方法在扩展 Vinyl 类时很有用。详细见下文 扩展 Vinyl

¥This method is useful when extending the Vinyl class. Detailed in Extending Vinyl below.

用法#

¥Usage

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

签名#

¥Signature

Vinyl.isCustomProp(property)

参数#

¥Parameters

参数typenote
propertystring要检查的属性名称。

返回#

¥Returns

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

¥True if the property is not internally managed.

扩展 Vinyl#

¥Extending Vinyl

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

¥When custom properties are managed internally, the static isCustomProp method must be extended and return false when one of the custom properties is queried.

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;
}
}

上例中,克隆时或传入 optionsnew SuperFile(options) 时,foo_foo 不会分配给新对象。

¥In the example above, foo and _foo will not be assigned to the new object when cloning or passed in options to new SuperFile(options).

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

¥If your custom properties or logic require special handling during cloning, override the clone method while extending Vinyl.