JavaScript 的奇葩知识

(1)
const arr = [1, 2, 3];
// 存在,等效于 > -1
if (~arr.indexOf(1)) {

}
// 不存在,等效于 === -1
!~arr.indexOf(1);

(2)
function fn1() {
  console.log(1);
}

function fn2() {
  console.log(2);
}

fn1.call.call(fn2); // 2
所以 fn1.call.call(fn2) 等效于 fn2.call(undefined)。而且无论您加多少个 .call,效果也是一样的。

(3)
0.3 - 0.2 !== 0.1  // true
浮点操作不精确,老生常谈了,不过可以接受误差

(4)
function Super() {
  this.a = 1;
}

function Child() {
  // 属性继承
  Super.call(this);
  this.b = 2;
}
// 原型继承
Child.prototype = new Super();

const child = new Child();
child.a;  // 1

正式代码的原型继承,不会直接实例父类,而是实例一个空函数,避免重复声明动态属性

(5)
{} === {} // false

// 隐式转换 toString()
{} >= {}  // true
对象 === 比较的是内存地址,而 >= 将比较转换后的值

(6)
Object.prototype.__proto__ === null; // true
这是原型链向上查找的最顶层,一个 null

(7)
parseInt(0.00000000454);  // 4
parseInt(10.23);          // 10
parseInt 太小的数字会产生 bug

(8)
1 + null          // 1
1 + undefined     // NaN

Number(null)      // 0
Number(undefined) // NaN

(9)
void 0 === undefined          // true
void 1 === undefined          // true
void {} === undefined         // true
void 'hello' === undefined    // true
void void 0 === undefined     // true
void 是个固执的老头


(10)
var timeout = setTimeout(() => console.log(1), 1000);
var interval = setInterval(() => console.log(2), 800);
clearTimeout 和 clearInterval 可以互换~~~~使用吗
clearInterval(timeout);
clearTimeout(interval);
答案是:YES 。大部分浏览器都支持互相清理定时器,但是建议使用对应的清理函数。

(11)
打印顺序
setTimeout(() => {
  console.log(1);
}, 0);

new Promise((resolve) => {
  console.log(2);
  resolve();
}).then(() => console.log(3));

function callMe() {
  console.log(4);
}

(async () => {
  await callMe();
  console.log(5);
})();
答案是:2, 4, 3, 5, 1

主线任务:2,4
微任务:3,5
宏任务:1

(12)
try/catch/finally 也有特定的执行顺序
function fn1() {
  console.log('fn1');
  return 1;
}

function fn2() {
  console.log('fn2');
  return 2;
}

function getData() {
  try {
    throw new Error('');
  } catch (e) {
    return fn1();
  } finally {
    return fn2();
  }
}

console.log(getData());

// 打印顺序: 'fn1', 'fn2', 2

(13)
//flat()方法最基本的作用就是数组降维
var arr1 = [1, 2, [3, 4]];
arr1.flat(); 
// [1, 2, 3, 4]
 
var arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]
 
var arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]
 
//使用 Infinity 作为深度,展开任意深度的嵌套数组
arr3.flat(Infinity); 
// [1, 2, 3, 4, 5, 6]

(14)
flatMap() 
var arr1 = [1, 2, 3, 4];
 
arr1.map(x => [x * 2]); 
// [[2], [4], [6], [8]]
 
arr1.flatMap(x => [x * 2]);
// [2, 4, 6, 8]
 
// 只会将 flatMap 中的函数返回的数组 “压平” 一层
arr1.flatMap(x => [[x * 2]]);
// [[2], [4], [6], [8]]

(15)
严格模式下,函数的 this 是 undefined 而不是 Window
// 非严格
function fn1() {
  return this;
}
fn1(); // Window

// 严格
function fn2() {
  'use strict';
  return this;
}
fn2(); // undefined

(16)
setTimeout 嵌套会发生什么奇怪的事情?
console.log(0, Date.now());

setTimeout(() => {
  console.log(1, Date.now());
  setTimeout(() => {
    console.log(2, Date.now());
    setTimeout(() => {
      console.log(3, Date.now());
      setTimeout(() => {
        console.log(4, Date.now());
        setTimeout(() => {
          console.log(5, Date.now());
          setTimeout(() => {
            console.log(6, Date.now());
          });
        });
      });
    });
  });
});
在0-4层,setTimeout 的间隔是 1ms ,而到第 5 层时,间隔至少是 4ms 。

(17)
es6函数带默认参数时将生成声明作用域
var x = 10;

function fn(x = 2, y = function () { return x + 1 }) {
  var x = 5;
  return y();
}
fn();   // 3

(18)
函数表达式(非函数声明)中的函数名不可覆盖
const c = function CC() {
  CC = 123;
  return CC;
};

c(); // Function
当然,如果设置 var CC = 123 ,加声明关键词是可以覆盖的。

(19)
构造函数如果 return了新的数据
function People() {}
const people = new People();   // People {}

// 返回数字
function People() {
  return 1;
}
const people = new People();   // People {}

// 返回新对象
function Animal() {
  return {
    hello: 'world',
  };
}
const animal = new Animal();  // { hello: 'world' }
在实例化构造函数时,返回非对象类型将不生效

以上内容转摘自:https://mp.weixin.qq.com/s/RDlG2WgsNKMIRNIARJHj9Q

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值