文章加密

;

2024年12月2日 星期一

Object.is() 与 == 运算符并不等价。 也不等价于 === 运算符

 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 视为彼此不相等。

constructor, __proto__, prototype

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



var name; // just declare
console.log(name); // => underfined

FooConstructor("JavaScript");  // 沒寫到new

// Oops! 怎麼被改到了??   
console.log(name);  // Javascript


可以改寫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}`);

  }

}


2024年12月1日 星期日

glob

 1. [] 塞選出任何符合這裏面放的字的項目

例如:

  • Cat.png
  • Bat.png
  • Rat.png
  • car.png
  • list.png
  • mom.jpg
  • cat.jpg

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:

  • Cat.png
  • Bat.png
  • Rat.png

2.
[!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:
  1. Globs are meant to match filenames rather than text
  2. Not all conventions are the same between them (example: * means zero or more copies of the same thing in regex)

monorepo: lerna + 創建私服 verdaccio + jest +eslint

https://www.bilibili.com/video/BV1s44y1C7an/?spm_id_from=333.337.search-card.all.click&vd_source=bfad58a748511d951ef1e6cc2082b1c8

  1. lerna 
  2. 創建私服 verdaccio 
  3. jest
  4. eslint
  5. prettier
  6. editconfig


 https://lerna.js.org/docs/getting-started


https://verdaccio.org/