2018-07-23javascript
1.關於函數中return與否的問題java
if (custom == undefined) { let content = content1; return content; } else { let content = custom.call(null, flight); return content; }
<span className={rowClassName}>{content}</span>
爲何頁面上直接返回了content值而沒有在 span中顯示?
並且,若是寫作函數
if (custom == undefined) { let content = content1; } else { let content = custom.call(null, flight); }
<span className={rowClassName}>{content}</span>
會提示說: content undefined?
爲何return以後沒有這個提示,不return 的話會報錯?this
採用三目運算就沒有問題
let content = custom == undefined ? (content = content1) : (content = custom.call(null, flight));
{content}spa
會正確在頁面上顯示content,這是爲何?prototype
2.lodash中each與map分別遍歷後的返回值:
使用each遍歷
返回code
let newColumns = new Array(); each(Columns, (c) => { let key = get(c, 'key'); let def = get(ColumnsDefine, key); let aa = extend({}, c, def); newColumns.push(aa); });
使用map遍歷:ip
let newColumns = map(Columns, (c) => { let key = get(c, 'key'); let def = get(ColumnsDefine, key); return extend({}, c, def); // newColumns.push(aa); });
目的是合併worker端傳來的Columns和ColumnsDefined裏的定義的Columns,最後使其
返回爲一個新的值newColumns.
可是最開始使用each並無成功返回我想要的合併後的值:
```javascript
let newColumns = each(Columns,(c) => {
let key = get(c, 'key');
let def = get(ColumnsDefine, key);
return extend({},c,def);
})get
爲何?it
先看一下lodash中的幾個方法: extend,each,map
extend 就是 assignIn的別名。
function Foo() { this.a = 1; } function Bar() { this.c = 3; } Foo.prototype.b = 2; Bar.prototype.d = 4; _.assignIn({ 'a': 0 }, new Foo, new Bar); // => { 'a': 1, 'b': 2, 'c': 3, 'd': 4 }
each就是forEach的別名。
_.forEach([1, 2], function(value) { console.log(value); }); // => Logs `1` then `2`. _.forEach({ 'a': 1, 'b': 2 }, function(value, key) { console.log(key); }); // => Logs 'a' then 'b' (iteration order is not guaranteed).
map方法:
function square(n) { return n * n; } _.map([4, 8], square); // => [16, 64] _.map({ 'a': 4, 'b': 8 }, square); // => [16, 64] (iteration order is not guaranteed) var users = [ { 'user': 'barney' }, { 'user': 'fred' } ]; // The `_.property` iteratee shorthand. _.map(users, 'user'); // => ['barney', 'fred']