_.toPlainObject(value)
170
_.toPlainObject(value)
_.toPlainObject将一个值转换成一个简单对象,展平继承的可枚举字符串键属性作为这个简单对象的自身属性
参数
value (*): 需要转换的值
返回值
(number): 返回转换好的简单对象
例子
function Foo() { this.b = 2; } Foo.prototype.c = 3; _.assign({ 'a': 1 }, new Foo); // => { 'a': 1, 'b': 2 } _.assign({ 'a': 1 }, _.toPlainObject(new Foo)); // => { 'a': 1, 'b': 2, 'c': 3 }
源代码
/** * Converts `value` to a plain object flattening inherited enumerable string * keyed properties of `value` to own properties of the plain object. * * @since 3.0.0 * @category Lang * @param {*} value The value to convert. * @returns {Object} Returns the converted plain object. * @example * * function Foo() { * this.b = 2 * } * * Foo.prototype.c = 3 * * assign({ 'a': 1 }, new Foo) * // => { 'a': 1, 'b': 2 } * * assign({ 'a': 1 }, toPlainObject(new Foo)) * // => { 'a': 1, 'b': 2, 'c': 3 } */ //将一个值转换成一个简单对象,展平继承的可枚举字符串键属性作为这个简单对象的自身属性 function toPlainObject(value) { value = Object(value) const result = {}//结果对象初始化 for (const key in value) {//for in 循环value result[key] = value[value]//为结果对象赋值 } return result } export default toPlainObject