https://w3c.hexschool.com/blog/a2cb755f
再舉例
data = [{
name: a,
value:10
|
const b =data.reduce((acc, item)=>{
acc[item.name] = item.value;
return acc
}, {})
console.log(b) // {a:10}
https://w3c.hexschool.com/blog/a2cb755f
再舉例
data = [{
name: a,
value:10
|
const b =data.reduce((acc, item)=>{
acc[item.name] = item.value;
return acc
}, {})
console.log(b) // {a:10}
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/is#%E6%B5%8F%E8%A7%88%E5%99%A8%E5%85%BC%E5%AE%B9%E6%80%A7
Object.is()
与 ==
运算符并不等价。==
运算符在测试相等性之前,会对两个操作数进行类型转换(如果它们不是相同的类型),这可能会导致一些非预期的行为,例如 "" == false
的结果是 true
,但是 Object.is()
不会对其操作数进行类型转换。
Object.is()
也不等价于 ===
运算符。Object.is()
和 ===
之间的唯一区别在于它们处理带符号的 0 和 NaN
值的时候。===
运算符(和 ==
运算符)将数值 -0
和 +0
视为相等,但是会将 NaN
视为彼此不相等。
https://www.lagagain.com/post/7%E5%A4%A9%E6%90%9E%E6%87%82js%E9%80%B2%E9%9A%8E%E8%AD%B0%E9%A1%8Cday02-new-factory/
題目
function FooConstructor(name){
this.name = name;
this.hello = function(){
console.log(`Hello, ${this.name}`);
}
}
var obj2 = new FooConstructor("Foo");
var obj3 = {};
obj3.constructor = FooConstructor;
obj3.constructor("Kitty");
obj3.hello(); // => Hello, Kitty
var obj4 = {};
obj4.__proto__ = FooConstructor.prototype;
obj4.constructor("K-on");
obj4.hello(); // => Hello, K-on
Object.is(obj3.constructor, obj4.constructor); // true
考題
FooConstructor.prototype.bye = function(){console.log(`Bye Bye, ${this.name}`)};
obj2.bye(); // => Bye Bye, Foo
// obj3.bye(); // error! 沒繼承到`FooConstructor`,不受影響
obj4.bye(); // => Bye Bye, K-on
可以改寫func如下避免:
The new.target meta-property lets you detect whether a function or constructor was called using the new operator.
function FooConstructor(name){
if (!new.target) throw "FooConstructor() must be called with new";
this.name = name;
this.hello = function(){
console.log(`Hello, ${this.name}`);
}
}
1. [] 塞選出任何符合這裏面放的字的項目
例如:
If you wanted to match only the title cased files in this list, you could use the pattern [CBR]at
.
This would return the result:
[!CBR]at
這個則是和上面的相反:
- car.png
- list.png
- mom.jpg
- cat.jpg
3.Backslashes are used to remove the special meaning of single characters '?'
, '*'
, and '['
, so that they can be used in patterns.
4.Asterisks (*)
The most common wildcard that you'll see is the asterisk. This character is used in many ways but is mainly used to match any number of characters (like parts of a string).
The three main use cases of asterisks that I've seen used are:
*
- On Linux, will match everything except slashes. On Windows, it will avoid matching backslashes as well as slashes.**
- Recursively matches zero or more directories that fall under the current directory.*(pattern_list)
- Only matches if zero or one occurrence of any pattern is included in the pattern-list above
These use cases can also be used in conjunction with each other! For example, to find all Markdown files recursively that end with .md
, the pattern would be **/*.md
Note: *.md
would only return the file paths in the current directory, which is why we append **/
at the beginning.
5.
Something important that I want to note is that while wildcard patterns are similar to regex patterns, they are not explicitly the same for two main reasons:- Globs are meant to match filenames rather than text
- Not all conventions are the same between them (example:
*
means zero or more copies of the same thing in regex)
https://www.bilibili.com/video/BV1s44y1C7an/?spm_id_from=333.337.search-card.all.click&vd_source=bfad58a748511d951ef1e6cc2082b1c8
https://lerna.js.org/docs/getting-started
https://verdaccio.org/
https://ithelp.ithome.com.tw/users/20128534/ironman/4377
https://ithelp.ithome.com.tw/articles/10264271
https://ithelp.ithome.com.tw/m/users/20103315/ironman/4764
function *g1(){
let products = ["Apple", "Banana", "Orange"]
for(var i in products){
yield products[i]
}
}
for(product of g1()){
console.log(product);
}
/* Output:
Apple
Banana
Orange
*/
注意到函式名稱前加了個星號(*
)了嗎?然後yield
配合上當下產生的產品。That's right! 就是這麼簡單。
var obj = g1();
console.log(obj); // => Object [Generator] {}
console.dir(obj.next()); // => { value: 'Apple', done: false }
console.dir(obj.next()); // => { value: 'Banana', done: false }
console.dir(obj.next()); // => { value: 'Orange', done: false }
console.dir(obj.next()); // => { value: undefined, done: true }
console.dir(obj.next()); // => { value: undefined, done: true }
生成器物件有next()
方法可以取的下一件物品。得到的值會是一個有value
和done
欄位。value
就是預取得的物件,done
在檢查是不是到盡頭沒材料了。
減少 Bug 並使得維護更加容易,比如:
String.prototype.toUppercase
以及 String.prototype.toUpperCase
,筆者到現在還是記不了到底字串要用哪種方法才能全部轉大寫,每次都要重新上網查挺麻煩的。但有了 TypeScript,它會自動幫我們提示要用後者 —— 也就是 toUpperCase
這個方法來避免錯誤。(如圖二)