主题
mixin
js
_.mixin([object=lodash], source, [options={}])
将源对象的所有自己的可枚举字符串键控函数属性添加到目标对象。如果 object
是一个函数,那么方法也会添加到其原型中。
¥Adds all own enumerable string keyed function properties of a source object to the destination object. If object
is a function, then methods are added to its prototype as well.
注意:使用 _.runInContext
创建一个原始 lodash
函数,以避免修改原始函数引起的冲突。
¥Note: Use _.runInContext
to create a pristine lodash
function to avoid conflicts caused by modifying the original.
新增于
¥Since
0.1.0
参数
¥Arguments
[object=lodash]
(函数|对象):目标对象。¥
[object=lodash]
(Function|Object): The destination object.source
(对象):要添加的函数对象。¥
source
(Object): The object of functions to add.[options={}]
(对象):填充长度。¥
[options={}]
(Object): The options object.[options.chain=true]
(布尔):指定 mixin 是否可链式。¥
[options.chain=true]
(boolean): Specify whether mixins are chainable.
返回
¥Returns
(*):返回 object
。
¥()*: Returns object
.
示例
¥Example
js
function vowels(string) {
return _.filter(string, function(v) {
return /[aeiou]/i.test(v);
});
}
_.mixin({ 'vowels': vowels });
_.vowels('fred');
// => ['e']
_('fred').vowels().value();
// => ['e']
_.mixin({ 'vowels': vowels }, { 'chain': false });
_('fred').vowels();
// => ['e']